且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将文本文件作为字符串读取?

更新时间:2023-11-07 20:05:34

text_file.readlines() 返回包含文件中行的字符串列表.如果您只需要一个字符串,而不是行列表,请改用 text_file.read().

text_file.readlines() returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read() instead.

您的代码中还有另一个问题,您正在尝试打开 unknown.txt,但您应该尝试打开 'unknown.txt'(一个字符串与文件名).

You also have another problem in your code, you are trying to open unknown.txt, but you should be trying to open 'unknown.txt' (a string with the file name).