fileMain = open("dictionary_15k.txt", "r")
for line1 in fileMain:
dictWords.append(unicode(line1.strip(), "utf-8"))
编译后会显示
NameError: name 'unicode' is not defined
fileMain = open("dictionary_15k.txt", "r")
for line1 in fileMain:
dictWords.append(unicode(line1.strip(), "utf-8"))
编译后会显示
NameError: name 'unicode' is not defined
在 Python 3 中没有这样的名称,你正在尝试在 Python 3 中运行 Python 2 代码。在 Python 3 中,unicode
已经被重命名为 str
。
然而,你可以完全删除 unicode()
调用;open()
返回一个已经将数据解码为 Unicode 的文件对象。你可能希望显式地告诉它使用什么编解码器:
fileMain = open("dictionary_15k.txt", "r", encoding="utf-8")
for line1 in fileMain:
dictWords.append(line1.strip())
如果你的教程是以Python 2为基础编写的,那么你可能希望切换到Python 2版本。