简体   繁体   English

函数内的 Python readline() 跳过文件中的第二行

[英]Python readline() inside function skips second line in file

I'm an absolute beginner in the programming field.我是编程领域的绝对初学者。 I'm doing some exercises from a book and there's something I don't understand.我正在做一些书上的练习,有些东西我不明白。 I'm using a readline() command inside a function and then starting a for loop in main() to find the largest number in a file.我在函数内使用 readline() 命令,然后在 main() 中启动 for 循环以查找文件中的最大数字。 The file has a list of numbers in each line: the largest number is in the second line, but for some reason the program skips the second line (which contains the largest number).该文件的每一行都有一个数字列表:最大的数字在第二行,但由于某种原因,程序跳过了第二行(其中包含最大的数字)。 This is the code:这是代码:

def reader(filename):
    nums = filename.readline()
    return(nums)

def main():
    filename = open("numbers.txt", 'r')
    largest = reader(filename)
    for line in filename:
        number = reader(filename)
        if number > largest:
            largest = number

    print("The largest number is:", largest)

main()

But when I put the readline() in the main() function and change the parameter inside the loop things work fine:但是当我将 readline() 放在 main() 函数中并更改循环内的参数时,一切正常:

def helper(filename):

    nums = filename
    return(nums)

def main():

    filename = open("numbers.txt", 'r')
    largest = helper(filename.readline())
    for line in filename:
        number = helper(line)
        if number > largest:
            largest = number

    print("The largest number is:", largest)

main()

There's obviously something that I'm missing but I don't know exactly what it is.显然我错过了一些东西,但我不知道它到底是什么。

for line in filename: reads each line in the file. for line in filename:读取for line in filename:中的每一行。

Calling readline() while inside such a loop, as you do in the first code sample, will cause the for loop to miss that line, as it was consumed by readline() and is no longer available to be consumed by the for loop. 与在第一个代码示例中所做的一样,在这样的循环内调用readline()会导致for循环丢失该行,因为该行已被readline()占用,而不再可供for循环使用。

The second code sample does not have this problem, because you are no longer calling readline() inside of a for line in file: loop. 第二个代码示例没有此问题,因为您不再for line in file:循环的for line in file:调用readline()

You are skipping all the even lines. 您正在跳过所有偶数行。 First you read a line: 首先,您读了一行:

largest = reader(filename)

then you read another one 然后你读另一本

for line in filename:

note line contains now the second line. 注意line现在包含第二行。 Finally, a 3d time: 最后,3d时间:

nums = filename.readline()

you completely disregard the second read, as you make no use of line . 您完全不理会二读,因为您没有使用line This of course will continue in the next loop - read the 4th line, disregard it and get the 5th etc. 当然,这将在下一个循环中继续-读取第4行,而忽略它并获得第5行,依此类推。

Also note you did not strip newlines (reading a line leaves a \\n ) or converting to int , so you are comparing strings. 另请注意,您没有剥离换行符(读取行会留下\\n )或转换为int ,因此您正在比较字符串。

def main():
    filename = open("numbers.txt", 'r')
    largest = -1
    for line in filename:
        if int(line) > int(largest):
            largest = line


    print("The largest number is:", largest)

main()

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

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