我用python处理日志文件。假设我有一个日志文件,其中包含一行START和一行END,如下所示:
START
one line
two line
...
n line
END
我想要的是能够在START和END线之间存储内容以便进一步处理。
我在Python中执行以下操作:
with open (file) as name_of_file:
for line in name_of_file:
if 'START' in line: # We found the start_delimiter
print(line)
found_start = True
for line in name_of_file: # We now read until the end delimiter
if 'END' in line: # We exit here as we have the info
found_end=True
break
else:
if not (line.isspace()): # We do not want to add to the data empty strings, so we ensure the line is not empty
data.append(line.replace(',','').strip().split()) # We store information in a list called data we do not want ','' or spaces
if(found_start and found_end):
relevant_data=data
然后我处理relevant_data。
看起来复杂的Python纯度,因此我的问题是:有更多的Pythonic方式吗?
要执行该操作,您可以使用iter(callable, sentinel)本文中讨论的内容,该内容将在您的案例“结束”(申请后.strip())中读取,直至达到sentinel value。
with open(filename) as file:
start_token = next(l for l in file if l.strip()=='START') # Used to read until the start token
result = [line.replace(',', '').split() for line in iter(lambda x=file: next(x).strip(), 'END') if line]
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。