Python unicode error
function, python, unicode
Solution
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:
>>> with open('workfile', 'r') as f:
... read_data = f.read()
>>> f.closed
True
Problem
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 and says ... Unresolved reference unicode for the first example ( i already seen a solution here but it didn't worked at all ) And says unresolved reference file, can anybody help me plz thks in advance ;) ``` def load(self, filename): try: f = open(filename, "rb") reader = csv.reader(f) for sub, pre, obj in reader: sub = unicode(sub, "UTF-8").encode("UTF-8") pre = unicode(pre, "UTF-8").encode("UTF-8") obj = unicode(obj, "UTF-8").encode("UTF-8") self.add(sub, pre, obj) f.close() print "Loaded data from " + filename + " !" except: print "Error opening file!" def save(self, filename): fnm = filename ; f = open(filename, "wb") writer = csv.writer(f) for sub, pre, obj in self.triples(None, None, None): writer.writerow([sub.encode("UTF-8"), pre.encode("UTF-8"), obj.encode("UTF-8")]) f.close() print "Written to " + filename ```