简体   繁体   English

python 中的 reading.txt 文件

[英]reading .txt file in python

I have a problem with a code in python.我对 python 中的代码有疑问。 I want to read a.txt file.我想读一个.txt 文件。 I use the code:我使用代码:

f = open('test.txt', 'r')  # We need to re-open the file
data = f.read()

print(data)

I would like to read ONLY the first line from this.txt file.我只想阅读 this.txt 文件的第一行。 I use我用

f = open('test.txt', 'r')  # We need to re-open the file
data = f.readline(1)

print(data)

But I am seeing that in screen only the first letter of the line is showing.但我看到在屏幕上只显示该行的第一个字母。

Could you help me in order to read all the letters of the line?你能帮我读一下这行的所有字母吗? (I mean to read whole the line of the.txt file) (我的意思是阅读整个 .txt 文件的行)

with open("file.txt") as f:
   print(f.readline())

This will open the file using with context block (which will close the file automatically when we are done with it), and read the first line, this will be the same as:这将使用with context 块打开文件(完成后将自动关闭文件),并读取第一行,这将与以下内容相同:

f = open(“file.txt”)
print(f.readline())
f.close()

Your attempt with f.readline(1) won't work because it the argument is meant for how many characters to print in the file, therefore it will only print the first character.您对f.readline(1)的尝试将不起作用,因为它的参数意味着要在文件中打印多少个字符,因此它只会打印第一个字符。

Second method:第二种方法:

with open("file.txt") as f:
   print(f.readlines()[0])

Or you could also do the above which will get a list of lines and print only the first line.或者您也可以执行上述操作,这将获取行列表并仅打印第一行。

To read the fifth line, use要阅读第五行,请使用

with open("file.txt") as f:
   print(f.readlines()[4])

Or:或者:

with open("file.txt") as f:
   lines = []
   lines += f.readline()
   lines += f.readline()
   lines += f.readline()
   lines += f.readline()
   lines += f.readline()
   print(lines[-1])

The -1 represents the last item of the list -1代表列表的最后一项

Learn more:学到更多:

Your first try is almost there, you should have done the following:您的第一次尝试就快到了,您应该执行以下操作:

f = open('my_file.txt', 'r')
line = f.readline()
print(line)
f.close()

A safer approach to read file is:读取文件的更安全方法是:

with open('my_file.txt', 'r') as f:
    print(f.readline())

Both ways will print only the first line.两种方式都只会打印第一行。

Your error was that you passed 1 to readline which means you want to read size of 1, which is only a single character.您的错误是您将1传递给readline ,这意味着您要读取 1 的大小,这只是一个字符。 please refer to https://www.w3schools.com/python/ref_file_readline.asp请参考https://www.w3schools.com/python/ref_file_readline.asp

I tried this and it works, after your suggestions:根据您的建议,我尝试了这个并且它有效:

f = open('test.txt', 'r')
data = f.readlines()[1]

print(data)

Use with open(...) instead: with open(...)使用:

with open("test.txt") as file:
    line = file.readline()
    print(line)

Keep f.readline() without parameters.保持f.readline()不带参数。

It will return you first line as a string and move cursor to second line.它会将第一行作为字符串返回,并将 cursor 移动到第二行。

Next time you use f.readline() it will return second line and move cursor to the next, etc...下次您使用f.readline()时,它将返回第二行并将 cursor 移动到下一行,等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM