【发布时间】:2019-01-09 17:49:34
【问题描述】:
我试图在 Python 2 上运行 python 3 代码,但它给了我这个错误:
TypeError: 必须是 unicode,而不是 str 我试过在 chr(i) 之前添加 str() 和在 "P" 之前添加 "u" 但我显然做错了。
tbl = dict.fromkeys(i for i in range(sys.maxunicode)
if unicodedata.category(chr(i)).startswith("P"))
def remove_punctuation(text):
return text.translate(tbl)
# initialize the stemmer
stemmer = LancasterStemmer()
# variable to hold the Json data read from the file
data = None
# read the json file and load the training data
with open('data.json') as json_data:
data = json.load(json_data)
print(data)
【问题讨论】:
-
在 Python 3 上运行它怎么样?
-
不能,因为 Tensorflow、Keras 和其他一切都可以在这台 PC 上的 Python 2 下正常工作。
-
你必须决定:Python 3 还是 Python 没有 Python 3 代码。顺便说一句,Tensorflow 和 Keras 在 Python 3.6 上运行良好。