How can I clean up text files in python?
I have text in a file that looks like this:
text1 5,000 6,000
text2 2,000 3,000
text3
5,000 3,000
text4 1,000 2000
text5
7,000 1,000
text6 2,000 1,000
Is there any way to clean this up in Python so that if there are missing numbers after a text line, the numbers on the subsequent line can be placed on the line above:
text1 5,000 6,000
text2 2开发者_Go百科,000 3,000
text3 5,000 3,000
text4 1,000 2000
text5 7,000 1,000
text6 2,000 1,000
Thanks!
Assuming there should be exactly three "words" on each line, you could use
tokens = (x for line in open("file") for x in line.split())
for t in zip(tokens, tokens, tokens):
print str.join(" ", t)
Edit: Since apparently the above prerequisite does not hold, here is an implementation that actually looks at the data:
from itertools import groupby
tokens = (x for line in open("file") for x in line.split())
for key, it in groupby(tokens, lambda x: x[0].isdigit()):
if key:
print str.join(" ", it)
else:
print str.join("\n", it),
Assuming that logical lines "continue" on lines which start with whitespace (and contain arbitrary amount of records), you can use this:
>>> collapse_space = lambda s: str.join(" ", s.split())
>>>
>>> logical_lines = []
>>> for line in open("text"):
... if line[0].isspace():
... logical_lines[-1] += line #-- append the continuation to the last logical line
... else:
... logical_lines.append(line) #-- start a new logical line
...
>>> l = map(collapse_space, logical_lines)
>>>
>>> print str.join("\n", l)
text1 5,000 6,000
text2 2,000 3,000
text3 5,000 3,000
text4 1,000 2000
text5 7,000 1,000
text6 2,000 1,000
精彩评论