我试图在python 3.5中练习从文件中读取文本,但我不断收到错误消息。
这是我的代码:
# A program that reads a text file and prints its contents on the screen
# so that each row contains a line number
# A reminder for UTF-8 users:
def insert_line_number(line, line_number):
"""
A function that takes a line and a corresponding line number and joins them
together.
:param line: A line of text
:param line_number: A corresponding line number
:return: A line with teh line number at the beginning
"""
line_with_number = "{:3d} {:s}".format(line_number, line)
return line_with_number
def print_lines(file):
"""
A function that reads lines from a file and prints them with an added line
number.
:param file: The file whose lines are to be printed
:return: -
"""
line_number = 0
try:
# Going through the lines, adding line numbers and printing the
# resulting line
for line in file:
# Removing unnecessary characters from the end of the line
line = line.rstrip()
line_number += 1
# Creating a line with the appropriate line number
line_with_number = insert_line_number(line, line_number)
print(line_with_number)
except IOError:
print("Error reading the file. Program will close.")
# Our main
def main():
filename = input("Syötä tiedoston nimi: ")
try:
file = open(filename, "r", encoding="utf-8")
print_lines(file)
file.close()
except IOError:
print("Error opening the file. Program will close.")
# Program start
main()
...以及这里的错误消息:
Traceback (most recent call last):
File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 55, in <module>
main()
File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 48, in main
print_lines(file)
File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 32, in print_lines
for line in file:
File "C:\Program Files (x86)\Python35-32\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 1: invalid continuation byte
Process finished with exit code 1
Thomas K在另一个帖子中说过:
&#34;换句话说:当你打开一个文件进行阅读时, 编码参数需要是文件已经编码的 in,而不是你想要的编码(你在编写时选择它 文件)&#34。 - Thomas K Nov 20&#39; 12 at 13:02
在此发布之前,我被特别指示使用encoding =&#34; utf-8&#34; - open() - 函数内的参数。没有参数,代码在我的机器上运行得很好,但是如果我包含它,我会得到上面的错误。
我有兴趣了解幕后发生的事情。为什么我的代码在包含参数时不起作用?