简体   繁体   English

Python seek()后跟readline()给出空字符串

[英]Python seek() followed by a readline() gives empty string

I am using something similar to the question here: 我在这里使用与问题类似的东西:

Python: Undo a Python file readline() operation so file pointer is back in original state Python:撤消Python文件readline()操作,以便文件指针恢复到原始状态

but seems to be misbehaving. 但似乎行为异常。

My code: (values at runtime shown as comments) 我的代码:(运行时的值显示为注释)

def parse_shell(self, filename):
    output = None
    with open(self.shells_path + filename) as fp:
        first_line = fp.readline()
# first_line = 'Last login: Tue Jun  9 07:13:26 on ttys0'
        while len(string.split(first_line, '$')) == 1:
            last_pos = fp.tell()
            # last_pos = 43
            first_line = fp.readline()
# first_line =' 2015-06-09 08:18:23 [coryj@Corys-MacBook-Pro ~]$ ping 10.2.2.1'

        fp.seek(last_pos)
        for line in fp.readline():
            # line = ''
            line_chunks = string.split(line, '$')

according to the docs, readline() should only return an empty string if it hits EOF. 根据文档,readline()仅在遇到EOF时才应返回空字符串。 This particular file is over 200k so I don't see why I would get an empty string. 这个特定的文件超过200k,所以我不明白为什么会得到一个空字符串。

The problem is that the statement: 问题是该语句:
while len(string.split(first_line, '$')) == 1: can not be True, so the script never will enter the while loop. while len(string.split(first_line, '$')) == 1:不能为True,因此脚本永远不会进入while循环。 string.split() will result in list of minimum two member, like ["some",""] , so len = 2 at least. string.split()将产生至少两个成员的列表,例如["some",""] ,因此len = 2至少。

Furthermore while loop will run while the statement True , and quits if it turn in to False . 此外,while循环将在语句True时运行,如果它变为False则退出。

I hope it helped! 希望对您有所帮助! Feel free to accept my answer if you feel it was useful to you. 如果您觉得我的回答对您有用,请随时接受。 :-) :-)

like o11c and lawrence said: 就像o11c和Lawrence所说:

for line in fp.readlines() is valid for line in fp.readlines()中的行有效

for line in fp is valid for line in fp中的行有效

for line in fp.readline() is not a valid construct. for line in fp.readline()不是有效的构造。

I don't get what is string.split ? 我不明白string.split什么? Do you mean str.split ? 您是说str.split吗? From what you are trying to do - it looks like - you want to split all lines that have $ in them into parts and ignore other lines. 从您要尝试执行的操作-看起来-您想将其中包含$所有行拆分为多个部分,并忽略其他行。 And in your particular case those lines seem to be at the beginning of file. 在您的特定情况下,这些行似乎在文件的开头。 A simpler approach would be 一个更简单的方法是

for line in fp:
    if line.find('$') >= 0:
        line.split('$')  # or str.split(line, '$')

Others have pointed out this already, still - line in fp.readlines() is different from line in fp.readline() . 其他人已经指出了这一点, line in fp.readlines()静止line in fp.readlines()中的line in fp.readline()不同。 Former would iterate over all lines of a file collected in a list, latter will iterate over all characters of only one line. 前者将遍历列表中收集的文件的所有行,后者将仅遍历一行的所有字符。

What you are trying to do is line in fp.readline() is actually trying to iterate over the contents of the line - which is probably not what you want. 您要执行的操作是line in fp.readline()中的行实际上是尝试遍历该行的内容-可能不是您想要的。

' 2015-06-09 08:18:23 [coryj@Corys-MacBook-Pro ~]$ ping 10.2.2.1'

So change it to line in fp (preferred) or line in fp.readlines() . 因此,将其更改为line in fp (首选)或line in fp.readlines() and you should be good 你应该很好

Hope that helps. 希望能有所帮助。

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

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