NameError: name 'unicode' not defined

fileMain = open("dictionary_15k.txt", "r") for line1 in fileMain: dictWords.append(unicode(line1.strip(), "utf-8")) 

When compiling, it shows

 NameError: name 'unicode' is not defined 
+6
source share
1 answer

There is no such name in Python 3, no. You are trying to run Python 2 code in Python 3. In Python 3, unicode been renamed str .

However, you can completely remove the unicode() call; open() creates a file object that already decodes Unicode data for you. You probably want to say which codec to use explicitly:

 fileMain = open("dictionary_15k.txt", "r", encoding="utf-8") for line1 in fileMain: dictWords.append(line1.strip()) 

You can switch to Python 2 if your tutorial is written with this version in mind.

+13
source

All Articles