Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'unicode' is not defined [duplicate]

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

When compiled it shows

NameError: name 'unicode' is not defined
like image 991
Soty Avatar asked Mar 20 '16 05:03

Soty


1 Answers

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

However, you can remove the unicode() call altogether; open() produces a file object that already decodes data to Unicode for you. You probably want to tell it what codec to use, explicitly:

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

You may want to switch to Python 2 if your tutorial is written with that version in mind.

like image 197
Martijn Pieters Avatar answered Sep 16 '22 16:09

Martijn Pieters