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[''])
这将正确解析示例文件:
结果:
^{pr2}$只需迭代文件,将每一行放入列表或哈希表中。然后迭代集合,对于每个匹配,使用match-1和-2的索引获取前两个条目。在
相关问题 更多 >
编程相关推荐