Python:使用dropwhile函数跳过注释行后读取第一条非注释行的问题

问题描述 投票:0回答:2

嗨,Stackoverflow偷看,

我需要阅读几个.dat文件并从中提取适当的信息。

。dat文件通常以大约15-25行注释开头(行以“#”开头),然后接下来的两行是表示其性质和大小的数字。但是,一旦我尝试读取第一条非注释行,我的代码似乎开始从第二条非注释行开始读取。我似乎无法弄清楚问题出在哪里://任何帮助将不胜感激,因为这使我无法使用这些.dat文件执行其他更复杂的操作。

这是我的错误代码:

PS:如果还有更Python化的方法,请让我知道:)

#Packages needed
from itertools import dropwhile

# In[]
#defining function to check if line starts with some character, here #
def is_comment(s):
    # return true if a line starts with #
    return s.startswith('#')

# In[]
file = "foo.dat"   

#viewing important info
with open(file, "r") as f:
    for line in dropwhile(is_comment, f):           

        Nat = f.readline()
        print("Nature:", Nat)

        S = f.readline()
        print("Size, S:", S)

        break

[请随附所附的foo.dat文件的屏幕截图。enter image description here

python itertools
2个回答
0
投票

with open('foo.dat') as f:
    lines = [l.strip() for l in f.readlines() if l[0] != '#']
    print(lines[0])
    print(lines[1])

0
投票

这是预期的

第一次进入循环时,line是第一个非注释行。您可以通过立即阅读另一行来跳过它。一个快速解决方案是use line

for line in dropwhile(is_comment, f):           
    Nat = line
    print("Nature:", Nat)

    S = f.readline()
    print("Size, S:", S)

更Python化的方式是手动迭代。

d = dropwhile(is_comment, f)
nat = next(d)
size = next(d)
© www.soinside.com 2019 - 2024. All rights reserved.