【发布时间】:2019-03-24 08:03:53
【问题描述】:
我正在尝试制作一个程序,该程序可以从 .txt 文件中读取行汉字并将它们打印到 Python shell(IDLE?)。
我遇到的问题是尝试对 utf-8 中的字符进行编码和解码,使其实际可以用中文打印。
到目前为止,我有这个:
file_name = input("Enter the core name of the text you wish to analyze:")+'.txt'
file = open(file_name, encoding="utf8")
file = file.read().decode('utf-8').split()
print(file)
但是,每次我运行代码时,我都会收到此错误提示。
file = file.read().decode('utf-8').split()
AttributeError: 'str' object has no attribute 'decode'
现在,我不完全确定这意味着什么,因为我是编程语言的新手,所以我想知道是否可以从你们那里得到一些提示。非常感谢!
【问题讨论】:
标签: python utf-8 foreign-keys decode encode