简体   繁体   中英

Printing unicode string works in python 3.2 but not in 2.7

I was using Python 3.2 to process text files with utf-8 text:

import codecs
import csv
f = codecs.open('07362853300091_trade_turquoise_errNo_031.csv', 
                                         'r','utf-8', 'ignore')
text = csv.reader(f, delimiter=',', quotechar='"')
for row in text:
    for item in row:
        print(item)

Worked fine.

I now have to run the code using Python 2.7 interpreter and it prints:

'\\xd7\\x97\\xd7\\x99\\xd7\\x95\\xd7\\x91 \\xd7\\x94\\xd7\\xa8 \\xd7\\xa2\\xd7\\xa6\\xd7\\x99\\xd7\\x95\\xd7\\x9f'

I tried

item.encode('utf-8')
print unicode(item, errors='ignore')

(and also tried some other combinations of encode() and unicode() functions) and it invariably prints:

u'\מ\ש\י\כ\ת \ש\י\ק'

How can I print the unicode text to the console in Python 2.7?

See unicode_csv_reader() in the docs .

As an alternative you could skip decoding/encoding if the console understands utf-8 and you don't do any text processing on the items other than printing them to console:

with open('07362853300091_trade_turquoise_errNo_031.csv', 'rb') as file:
     for row in csv.reader(file):
         print "\n".join(row)

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