Accent error in Python 2.7

Asked

Viewed 585 times

-2

During coding (below) there is an error regarding the accentuation of some words:

Note: I am using Notepad++ and running python 2.7 on windows cmd

Code:

# encoding: utf-8
import os, sys

print "EXPEDIÇÃO DE DESPACHO"

Exit:

λ python teste1.py
EXPEDIÇÃO DE DESPACHO

Imagery inserir a descrição da imagem aqui

Note: I checked this question in another post but it did not help: Encoding utf-8 allows accents?

1 answer

1


Apparently in cmd with Python2.7 just use the u"...", thus:

print u"á é í"

Note that the document should also be saved in utf-8 without BOM

Using Notepad++:

utf8 sem boom notepad++

Using Sublime Text:

utf8 sublime sublimetext

I tested in the cmd.exe native, but not working, I will edit the answer as soon as I get a technical solution and a detail on what changes from python2.7 and 3.x, because 3.x works correctly without using the command chcp.

cmd did not work, but Cmder worked using the command chcp 65001 before you run python, then you can do this directly via script, example:

# -*- coding: utf-8 -*-
import os, sys

os.system('@chcp 65001')

print "EXPEDIÇÃO DE DESPACHO"
print "expedição de despacho"

Upshot:

Acentos no cmder com python2.7

  • William, the first answer worked on Cmder. # -- coding: utf-8 -- import os, sys os.system('@chcp 65001') print "DISPATCH"

  • As for the second version no!

  • I couldn’t find an explanation for this fact. Strange to notice this type of behavior in python 2.7

  • @alexjosesilva the use of u worked on both cmd.exe and Cmder here so print u"á é í", using Python2.7, close cmd and Cmder and try again, remember, you have to save the script as utf-8 as well ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.