Input:
@example1
abcd
efg
hijklmnopq
@example2
123456789
Script:
def parser_function(f):
name = ''
body = ''
for line in f:
if len(line) >= 1:
if line[0] == '@':
name = line
continue
body = body + line
yield name,''.join(body)
for line in parser_function(data_file):
print line
Output
('@example1', 'abcd')
('@example1', 'abcdefg')
('@example1', 'abcdefghijklmnopq')
('@example2', 'abcdefghijklmnopq123456789')
Desired Output:
('@example1', 'abcdefghijklmnopq')
('@example2', '123456789')
My problem, my generator is yielding every line but i'm not sure where to reset the line. i'm having trouble getting the desired output and i've tried a few different ways. any help would be greatly appreciated. saw some other generators that had "if name:" but they were fairly complicated. I got it to work using those codes but i'm trying to make my code as small as possible