python - 连续行满足多个条件时的文件解析
问题描述
我正在尝试解析具有如下数据的文本文件:
============================= condition 1 ============================ condition 2 string col 1 col2 tags ------------------------------ xx xx abc xx xx ac col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13 ----------------------------------------------------------------------------------------------------- 1 11 6 30 abc text -2794 682 57388 294 210 2 11 6 30 ac text -2447 680 52973 302 214 3 11 13 text -2619 -805 120956 568 255 4 11 16 text 2185 -1261 116983 548 273 5 11 17 text -3362 -1413 127136 569 278 Criterion 30 : xxxxx text3 11 : some text here ============================================================================
以下是我想做的事情
- 查找条件 1,如果满足,请确保存在条件 2 字符串
- 选择“标签”列中的值
- 然后在下表中查找这些标签以从第 9 到 13 列中提取信息。
我可以做第三部分,但是,当我使用 f.next() 检查条件 2 时,我正在努力解决前两个问题,它弄乱了我的代码:
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# - F# 如何从 AND 运算返回布尔值?
- javascript - 如何在css的内联块中显示垂直溢出的项目
- flutter - 防止触摸事件到达父小部件
- json - 使用 arm 模板部署存储帐户时遇到问题
- php - 将值传递到 ACF 前端表单字段
- clickhouse - 如何获得低于所选日期的最大值?
- python - 如何在 Django 中只显示表单中的一个字段
- flutter - 显示错误:内部错误:(java.lang.ClassNotFoundException) com.google.wireless.android.sdk.stats.IntellijIndexingStats$Index
- php - 使用 PHP 从 Google 表格中获取单元格格式信息
- nginx - 内容安全策略中的空 http 和 https 声明