Python 连续行中满足多个条件时的文件解析
我正在尝试解析包含以下数据的文本文件: 下面是我想做的事情Python 连续行中满足多个条件时的文件解析,python,for-loop,fileparsing,Python,For Loop,Fileparsing,我正在尝试解析包含以下数据的文本文件: 下面是我想做的事情 查找条件1,如果满足,确保条件2字符串存在 选择“标记”列中的值 然后在下表中查找这些标记,从第9列到第13列中提取信息 我可以做第三部分,但是,我正在努力处理前两部分,因为当我使用f.next()检查条件2时,它正在混乱我的代码: with open(each_file) as f: copy = False i = 0 for linenum,line in enumerate(f): if
with open(each_file) as f:
copy = False
i = 0
for linenum,line in enumerate(f):
if line.strip() == "============================= Condition 1 ============================":
line_next = f.next()
if line_next.strip() == "condition 2 string":
print "here1"
print line.strip()
copy = True ## Start copying when both the conditions are met
elif line_next.strip() == "col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13": ## Stop copying at this line
if i == 0:
copy = False
else:
copy = False
i = i + 1
elif copy:
print copy
print line
请帮我解决这个问题。这应该可以满足您的要求:
with open(each_file) as f:
cond_1 = False
copy = False
for linenum,line in enumerate(f.readlines()):
line = line.strip()
#print("DEBUG: line is <{0}>".format(line))
if line == "============================= condition 1 ============================":
print "DEBUG: condition 1"
cond_1 = True
elif cond_1 and line == "condition 2 string":
print "DEBUG: condition 2 / start copying"
copy = True ## Start copying when both the conditions are met
elif line == "col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13": ## Stop copying at this line
print "DEBUG: stop copying"
copy = False
if copy:
#print "DEBUG: Copying..."
print line
打开(每个_文件)作为f:
条件1=错误
复制=错误
对于linenum,枚举中的行(f.readlines()):
line=line.strip()
#打印(“调试:行为”。格式(行))
如果行==“==============================================================================================================================================”:
打印“调试:条件1”
cond_1=真
elif cond_1和line==“条件2字符串”:
打印“调试:条件2/开始复制”
copy=True##当两个条件都满足时开始复制
elif line==“col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13”:##在这一行停止复制
打印“调试:停止复制”
复制=错误
如果是副本:
#打印“调试:复制…”
打印行
我认为您正确地识别了您的问题(f.next()
正在弹出,而不是偷看)。如果文件不太大,您可以将其全部读入内存(例如,作为列表行
),然后选中行[linenum+1]
。或者,您可以在找到“===条件1===”时设置一个标志,然后在下一次迭代中,检查标志和“条件2”是否都存在。除了最大的输入文件外,我更喜欢第一个选项,这将导致更干净的代码,IMHO。可能重复@jedwards的标志想法,非常感谢:)
with open(each_file) as f:
cond_1 = False
copy = False
for linenum,line in enumerate(f.readlines()):
line = line.strip()
#print("DEBUG: line is <{0}>".format(line))
if line == "============================= condition 1 ============================":
print "DEBUG: condition 1"
cond_1 = True
elif cond_1 and line == "condition 2 string":
print "DEBUG: condition 2 / start copying"
copy = True ## Start copying when both the conditions are met
elif line == "col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13": ## Stop copying at this line
print "DEBUG: stop copying"
copy = False
if copy:
#print "DEBUG: Copying..."
print line