Reading Unicode Files - Python3.2

I am trying to read some files using Python3.2, some of the files may contain unicode, while others may not.

When I try:

file = open(item_path + item, encoding="utf-8")
for line in file:
    print (repr(line))

I get an error message:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-16: ordinal not in range(128)

I follow the documentation here: http://docs.python.org/release/3.0.1/howto/unicode.html

Why is Python trying to code ascii anywhere in this code?

+3
source share
2 answers

What is your output encoding? If you delete the call on print(), will it start working?

I suspect you have a non-UTF-8 locale, so Python is trying to code repr(line)as ASCII as part of its print. A.

, , , , (UTF-8 ).

+2

, repr(line) Python 3 Unicode. 128 escape- ASCII.

ascii(line) , escape-.

, repr(line) , , , . , Python 3 , escape- ASCII , ASCII. UTF-8 Unicode . , Python 2 escape- .

+3

All Articles