Reputation: 3249
I have been trying for hours to solve this UTF-8 issue in Python 2.7.6.
I have a list of string with UTF-8 characters, like this:
findings=['Quimica Geral e Tecnol\xf3gica I', 'Quimica Geral e Tecnol\xf3gica II', '\xc1lgebra Linear']
I am trying to print the strings:
for finding in findings:
print finding
The output is:
Quimica Geral e Tecnolgica I
Quimica Geral e Tecnolgica II
lgebra Linear
I also tried this:
for finding in findings:
print( "%s"%(finding))
and I got the same output.
If I try to save in file:
file = open("teste.txt", "w")
for finding in findings:
file.write("%s\n" % finding)
file.close()
It works and the output is (please note the latin characters - accents):
Quimica Geral e Tecnológica I
Quimica Geral e Tecnológica II
Álgebra Linear
What Am I doing wrong?
Upvotes: 1
Views: 2689
Reputation: 107347
You need to convert your strings to unicode with unicode
function and use unicode-escape
( Produce a string that is suitable as Unicode literal in Python source code )as your encodeing :
>>> for i in findings :
... print unicode(i,'unicode-escape')
...
Quimica Geral e Tecnológica I
Quimica Geral e Tecnológica II
Álgebra Linear
Added : The I/O system is built as a series of layers and when you open a file for writing it use io.TextIOWrapper
layer that is a text-handling layer that encodes and decodes Unicode automatically .
Upvotes: 2