简体   繁体   English

在函数中使用readline()来读取日志文件不会进行迭代

[英]using readline() in a function to read through a log file will not iterate

In the code below readline() will not increment. 在下面的代码中, readline()不会递增。 I've tried using a value, no value and variable in readline() . 我试过在readline()使用值,无值和变量。 When not using a value I don't close the file so that it will iterate but that and the other attempts have not worked. 当不使用值时,我不会关闭文件,以便它可以迭代,但是其他尝试均无效。

What happens is just the first byte is displayed over and over again. 发生的只是第一个字节一遍又一遍地显示。

If I don't use a function and just place the code in the while loop (without 'line' variable in readline() ) it works as expected. 如果我不使用函数,而只是将代码放在while循环中( readline()没有'line'变量),它将按预期工作。 It will go through the log file and print out the different hex numbers. 它将通过日志文件并打印出不同的十六进制数字。

i=0
x=1

def mFinder(line):
    rgps=open('c:/code/gps.log', 'r')
    varr=rgps.readline(line)
    varr=varr[12:14].rstrip()
    rgps.close()
    return varr


while x<900:
    val=mFinder(i)
    i+=1
    x+=1
    print val
    print 'this should change'

It appears you have misunderstood what file.readline() does. 看来您误解了file.readline()作用。 Passing in an argument does not tell the method to read a specific numbered line. 传递参数不会告诉方法读取特定编号的行。

The documentation tells you what happens instead: 该文档告诉您发生了什么:

file.readline([size])
Read one entire line from the file. 从文件中读取整行。 A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). 字符串中保留尾随换行符(但是,如果文件以不完整的行结尾,则可能不存在)。 If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. 如果size参数存在且为非负数,则为最大字节数(包括结尾的换行符),并且可能返回不完整的行。

Bold emphasis mine, you are passing in a maximum byte count and rgps.readline(1) reads a single byte, not the first line. 粗体强调,您传入的是最大字节数,并且rgps.readline(1)读取单个字节,而不是第一行。

You need to keep a reference to the file object around until you are done with it, and repeatedly call readline() on it to get successive lines. 在完成处理之前,您需要保留对文件对象的引用,并重复调用该文件对象上的readline()以获取连续的行。 You can pass the file object to a function call: 您可以将文件对象传递给函数调用:

def finder(fileobj):
    line = fileobj.readline()
    return line[12:14].rstrip()

with open('c:/code/gps.log') as rgps:
    x = 0
    while x < 900:
        section = finder(rgps)
        print section
        # do stuff
        x += 1

You can also loop over files directly , because they are iterators: 您还可以直接循环遍历文件,因为它们是迭代器:

for line in openfilobject:

or use the next() function to get a next line, as long as you don't mix .readline() calls and iteration (including next() ). 或使用next()函数获得下一行,只要您不将.readline()调用和迭代(包括next() )混合使用即可。 If you combine this witha generator function , you can leave the file object entirely to a separate function that will read lines and produce sections until you are done: 如果将此与生成器函数结合使用,则可以将文件对象完全留给一个单独的函数,该函数将读取行并生成节,直到完成:

def read_sections():
    with open('c:/code/gps.log') as rgps:
        for line in rgps:
            yield line[12:14].rstrip()

for section in read_sections():
    # do something with `section`.

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

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