我有一个文本文件,里面有几千行文字。下面给出了一个样本:
person1
person2
person3
person4
have paid
---------
person5
person6
person7
person9
person10
person11
have paid
---------
每行以“p”或“h”或“ - ”开头。当在阅读文件时遇到“已付款”时,我想将前两行附加到列表中,以便我可以区分已付款的人和未付款的人。有什么帮助吗?
干杯, chav的
答案 0 :(得分:1)
data=open("file").read().split("\n\n")
for rec in data:
if "have paid" in rec:
print rec.split("have paid")[0]
答案 1 :(得分:0)
只需迭代文件,将每一行放入List或哈希表中。然后迭代集合,并为每个匹配使用匹配-1和-2的索引获取前两个条目。
答案 2 :(得分:0)
这会正确解析您的示例文件:
with open('yourfile') as f:
result = {'have paid': [], '': []}
current = []
for line in f:
line = line.strip().strip('-')
if line in result:
result[line].extend(current)
current = []
else:
current.append(line)
print 'These have paid:', ','.join(result['have paid'])
print 'These have not paid:', ','.join(result[''])
结果:
These have paid: person3,person4,person10,person11
These have not paid: person1,person2,person5,person6,person7,person9