简体   繁体   中英

Python: How to print with UTF-8 characters?

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM