UnicodeDecodeError:“utf-8”编解码器无法解码位置 1 中的字节 0xe4:无效的连续字节

SeS*_*esa 5 unicode encoding utf-8 python-3.x

我正在尝试练习从 python 3.5 中的文件中读取文本,但我不断收到错误消息。

\n\n

这是我的代码:

\n\n
# A program that reads a text file and prints its contents on the screen\n# so that each row contains a line number\n# A reminder for UTF-8 users:\n\n\ndef insert_line_number(line, line_number):\n    """\n    A function that takes a line and a corresponding line number and joins them\n    together.\n\n    :param line: A line of text\n    :param line_number: A corresponding line number\n    :return: A line with teh line number at the beginning\n    """\n    line_with_number = "{:3d} {:s}".format(line_number, line)\n    return line_with_number\n\n\ndef print_lines(file):\n    """\n    A function that reads lines from a file and prints them with an added line\n    number.\n\n    :param file: The file whose lines are to be printed\n    :return: -\n    """\n    line_number = 0\n    try:\n        # Going through the lines, adding line numbers and printing the\n        # resulting line\n        for line in file:\n            # Removing unnecessary characters from the end of the line\n            line = line.rstrip()\n            line_number += 1\n            # Creating a line with the appropriate line number\n            line_with_number = insert_line_number(line, line_number)\n            print(line_with_number)\n    except IOError:\n        print("Error reading the file. Program will close.")\n\n\n# Our main\ndef main():\n    filename = input("Sy\xc3\xb6t\xc3\xa4 tiedoston nimi: ")\n    try:\n        file = open(filename, "r", encoding="utf-8")\n        print_lines(file)\n        file.close()\n    except IOError:\n        print("Error opening the file. Program will close.")\n\n\n# Program start\nmain()\n
Run Code Online (Sandbox Code Playgroud)\n\n

...这是错误消息:

\n\n
Traceback (most recent call last):\n  File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 55, in <module>\n    main()\n  File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 48, in main\n    print_lines(file)\n  File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 32, in print_lines\n    for line in file:\n  File "C:\\Program Files (x86)\\Python35-32\\lib\\codecs.py", line 321, in decode\n    (result, consumed) = self._buffer_decode(data, self.errors, final)\nUnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0xe4 in position 1: invalid continuation byte\n\nProcess finished with exit code 1\n
Run Code Online (Sandbox Code Playgroud)\n\n

Thomas K 在另一个帖子中这样说道:

\n\n
\n

“换句话说:当您打开文件进行读取时,\n 编码参数需要是文件已经\n 的编码,而不是您想要的编码(您在写入时选择\n文件)。"\n \xe2\x80\x93 Thomas K 2012 年 11 月 20 日 13:02

\n
\n\n

在在这里发布此内容之前,我被特别指示在 open() 函数中使用encoding="utf-8" 参数。如果没有该参数,代码在我的机器上运行得很好,但如果我包含它,则会收到上面的错误。

\n\n

我很想知道幕后发生了什么。为什么包含参数时我的代码不起作用?

\n