简体   繁体   English

无法使用 readline() 在 python 中读取多行文件

[英]Unable to read multiline files in python using readline()

The following code is not working properly.以下代码无法正常工作。 It is unable to read multiline files in python using readline().无法使用 readline() 在 python 中读取多行文件。

myobject=open("myfile.txt",'r')
while ((myobject.readline())):
    print(myobject.readline())
myobject.close()

It just prints the first line and then newlines.它只打印第一行,然后是换行符。 I don't understand why?我不明白为什么?

When you open the file in 'r' mode, the file object returned points at the beginning of the file.当您以'r'模式打开文件时,文件对象返回指向文件开头的点。

Everytime you call readline, a line is read, and the object now points to the next line in the file每次调用 readline 时,都会读取一行,并且对象现在指向文件中的下一行

Since your loop condition also reads the file and moves it to the next line, you are getting lines only at even places, like line no 2, 4, 6. Line Numbers, 1, 3, 5, ... will be read by while ((myobject.readline())): and discarded.由于您的循环条件也会读取文件并将其移动到下一行,因此您只会在偶数位置获取行,例如第 2、4、6 行。行号、1、3、5……将被读取while ((myobject.readline())):并丢弃。

A simple solution will be一个简单的解决方案是

myobject = open("myfile.txt",'r')
for line in myobject:
    print(line, end='')
myobject.close()

OR for your case, when you want to use only readline()或者对于您的情况,当您只想使用 readline()

myobject = open("myfile.txt",'r')

while True:
    x = myobject.readline()
    if len(x) == 0:
        break
    print(x, end='')

myobject.close()

This code works, because readline behaves in the following way.此代码有效,因为 readline 的行为方式如下。

According to python documentation, https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects根据python文档, https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

f.readline() reads a single line from the file; f.readline() 从文件中读取一行; a newline character (\\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn't end in a newline.换行符 (\\n) 留在字符串的末尾,如果文件不以换行符结尾,则仅在文件的最后一行省略。 This makes the return value unambiguous;这使得返回值明确; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\\n', a string containing only a single newline.如果 f.readline() 返回空字符串,则表示已到达文件末尾,而空行由 '\\n' 表示,该字符串仅包含一个换行符。

It's because readline reads one line at a time, your code will still print a new line because readline keeps trailing newlines.这是因为readline读取一行,您的代码仍然会打印一个新行,因为readline会一直尾随换行符。

The way to fix would be to do this:解决方法是这样做:

with open("myfile.txt", 'r') as f:
    for line in f:
        print(line)

readline() returns the line that it is currently pointing to and moves to the next line. readline()返回它当前指向的行并移动到下一行。 So, the calls to the function in the while condition and in the print statement are not the same.因此,while 条件和print 语句中对函数的调用是不一样的。 In fact, they are pointing to adjacent lines.事实上,它们指向相邻的线。

First, store the line in a temporary variable, then check and print.首先,将该行存储在一个临时变量中,然后检查并打印。

myobject = open('myfile.txt')
while True:
    line = myobject.readline()
    if line:
        print(line)
    else:
        break

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

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