Skip to content Skip to sidebar Skip to footer

Python Unicode Error

I have a program made in python that was built to python 2 , but now i have to rebuild it and have already change some things to python3 but somehow, my csv is not being loaded an

Solution 1:

unicode(sub, "UTF-8")

should be

sub.decode("UTF-8")

Python3 unified the str and unicode types so there's no longer a builtin unicode cast operator.


The Python 3 Unicode HOWTO explains a lot of the differences.

Since Python 3.0, the language features a str type that contain Unicode characters, meaning any string created using "unicode rocks!", 'unicode rocks!', or the triple-quoted string syntax is stored as Unicode.

and explains how encode and decode relate to one another

Converting to Bytes

The opposite method of bytes.decode() is str.encode(), which returns a bytes representation of the Unicode string, encoded in the requested encoding.


Instead of

file(...)

use open

The I/O docs explain how to use open and how to use with to make sure it gets closed.

It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:

 >>> withopen('workfile', 'r') as f:
 ...     read_data = f.read()
 >>> f.closed
 True

Post a Comment for "Python Unicode Error"