Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing unicode string works in python 3.2 but not in 2.7

Tags:

python

unicode

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'\u05de\u05e9\u05d9\u05db\u05ea \u05e9\u05d9\u05e7'

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

like image 499
Jack Kaymans Avatar asked Jan 01 '26 20:01

Jack Kaymans


1 Answers

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)
like image 92
jfs Avatar answered Jan 03 '26 09:01

jfs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!