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

Reading Chinese text from a file and printing it to the shell

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

文件=打开(file_name,编码="utf8")

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

data = file.read().split()