简体   繁体   English

根据条件附加文件的前几行

[英]Append previous lines of a file based on a condition

I have a text file with a few 1000 lines of text in it. 我有一个文本文件,其中包含几千行文本。 A sample is given below: 下面是一个示例:

person1
person2

person3
person4
have paid
---------

person5
person6

person7
person9

person10
person11
have paid
---------

Each line starts with either "p" or "h" or "-". 每行以“ p”或“ h”或“-”开头。 When "have paid" is encountered while reading the file, I want to append the previous two lines into a list so that I can differentiate people who have paid and people who have not paid. 当在读取文件时遇到“已付款”时,我想将前两行添加到列表中,以便区分已付款的人和未付款的人。 Any help? 有什么帮助吗?

Cheers, Chav 干杯,Chav

data=open("file").read().split("\n\n")
for rec in data:
    if "have paid" in rec:
         print rec.split("have paid")[0]

Just iterate the file putting every line into a List or a hashtable. 只需迭代文件,将每一行放入列表或哈希表即可。 Then iterate the collection and for each match grab the two previous entries using the index of match -1 and -2. 然后迭代该集合,并为每个匹配使用匹配-1和-2的索引获取前两个条目。

This parses correctly your example file: 这可以正确解析您的示例文件:

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[''])

Result: 结果:

These have paid: person3,person4,person10,person11
These have not paid: person1,person2,person5,person6,person7,person9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM