从文件中读取中文文本并将其打印到外壳中



我正在尝试制作一个程序,该程序可以从.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'

现在,我不完全确定这意味着什么,因为我是编程语言的新手,所以我想知道我是否可以从你们那里得到一些提示。非常感谢!

从您的错误消息中,我怀疑.read()的输出已经是一个字符串(更准确地说,如果您使用的是Python 3,则为 unicode 字符点(。

您是否在没有.decode()电话的情况下尝试过?


为了更好地处理文件,请使用with上下文,因为这可确保在退出块后正确关闭文件。 此外,还可以使用for line in f语句循环访问文件中的行。

file_name = input("Enter the core name of the text you wish to analyze:")
with open(file_name + '.txt', encoding='utf8') as f:
for line in f:
line = line.strip()   # removes new lines or spaces at the start/end
print(line)

当你读取在Python 3中以这种方式打开的文件时:

文件 = open(file_name, encoding="utf8"(

你告诉它文件是用UTF-8编码的,Python会自动解码它。file.read()已经是一个 Unicode 字符串(Python 3 中的str类型(,所以你不能再次解码它。 只需执行以下操作(并且不要覆盖file...那是你的文件句柄(:

data = file.read().split()

最新更新