Python textfile formatting -
here text file
apple1 apple2 apple3 date apple flower1 flower2 flower3 flower4 date flower dog1 dog2 date dog
i need python code helps me turn file this
apple1|date apple apple2|date apple apple3|date apple flower1|date flower flower2|date flower flower3|date flower flower4|date flower dog1|date dog dog2|date dog
it need nested loop, 1 counts until line.startswith "date" when gets there appends every line before counter starts on while x between range of 0 , total line count. ideas?
my solution requires list contains things not start date.
f = open('apple.txt') lines = f.readlines() f.close() things = [] printtofile = [] in lines: things.append(i) if i.startswith('date'): things.pop() x in things: if i[:-1] == '\n': printtofile.append(x[:-1]+'|'+i[:-1]) else: printtofile.append(x[:-1]+'|'+i) things = [] print printtofile writefile = open('file.txt', 'w') writefile.writelines(printtofile) writefile.close()
hope helps, python 2.7
Comments
Post a Comment