Analysis and output of CSV file in Python

advertisements

I am trying to parse a text file which has the following format :

+++++
line1
line2
<<<<<
+++++
rline1
rline2
<<<<<

where, +++++ means start of record and <<<<< means end of record.

Now I want to output the whole texts into csv in the following format:

line1, line2
rline1, rline2

I am trying sth like this:

lines =['+++++', 'line1', 'line2', '<<<<<', '+++++', 'rline1', 'rline2', '<<<<<']
output_lines =[]

for line in lines:
    if (line == "+++++") or not(line == "<<<<<") :
        if (line == "<<<<<"):
            output_lines.append(line)
            output_lines.append(",")

print (output_lines)

I am not sure how to move forward from here.


maybe something like this?

from itertools import groupby
import csv

lines =['+++++', 'line1', 'line2', '<<<<<', '+++++', 'rline1', 'rline2', '<<<<<']

# remove the +++++s, so that only the <<<<<s indicate line breaks
cleaned_list = [ x for x in lines if x is not "+++++" ]

# separate at <<<<<s
rows = [list(group) for k, group in groupby(cleaned_list, lambda x: x == "<<<<<") if not k]

f = open('result.csv', 'wt')
try:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)
finally:
    f.close()

print open('result.csv', 'rt').read()