我需要阅读几个.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文件的屏幕截图。foo.dat或此处的文字版本:https://pastebin.com/C91dpUXK*编辑:添加了指向文本文件的链接
这是预期的
第一次进入循环时,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)
这里您可以提取不为空或注释的行(以#
开头:]
with open('foo.dat') as f:
no_comment_lines = [l.strip() for l in f.readlines()
if l.strip() and not l.startswith('#')]
for l in no_comment_lines:
print(l)
下面(假设您需要所有文件行)
with open('foo.dat') as f:
lines = [l.strip() for l in f.readlines() if l[0] != '#']
print(lines[0])
print(lines[1])
for line in lines[2:]:
fields = [f for f in line.split(' ') if len(f)]
print(fields)