views:

24

answers:

3

Hi, 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 "-". 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

A: 

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.

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

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
nosklo