【发布时间】:2020-08-22 07:23:56
【问题描述】:
我对 python 中的代码有疑问。我想阅读一个 .txt 文件。我使用代码:
f = open('test.txt', 'r') # We need to re-open the file
data = f.read()
print(data)
我只想读取这个 .txt 文件的第一行。我用
f = open('test.txt', 'r') # We need to re-open the file
data = f.readline(1)
print(data)
但我看到屏幕上只显示了该行的第一个字母。
你能帮我读一下这行的所有字母吗? (我的意思是阅读整个 .txt 文件的行)
【问题讨论】:
-
在
f = open('test.txt', 'r')之后,然后是one_line = next(f)。