python - How can I add the first line in a text file to following lines until a condition is met then repeat? -
i have form of data in text file.
ub25x
0060 4/22/16 -20.19
0060 3/17/15 -23.37
*ub25x
fj39y
0060 1/15/16 -27.34
0060 7/15/16 -23.10
*fj39y
i want print output:
ub25x 0060 4/22/16 -20.19
ub25x 0060 3/17/15 -23.37
fj39y 0060 1/15/16 -27.34
fj39y 0060 7/15/16 -23.10
you can use this:
input = '''ub25x 0060 4/22/16 -20.19 0060 3/17/15 -23.37 *ub25x fj39y 0060 1/15/16 -27.34 0060 7/15/16 -23.10 *fj39y''' l = input.split('\n') tag = none out = [] item in l: if tag == item.replace('*',''): tag = none elif tag: out.append(tag + ' ' + item) else: tag = item print out # ['ub25x 0060 4/22/16 -20.19', 'ub25x 0060 3/17/15 -23.37', 'fj39y 0060 1/15/16 -27.34', 'fj39y 0060 7/15/16 -23.10']
Comments
Post a Comment