Reputation: 149
I'm looking for a nice pythonic way of reading a file, and joining any lines which are logical continuations of the ones above, as indicated by a line continuation character. E.g.
Here is a normal line.
This line continues over \
two lines.
This line continues over\
three \
lines.
I've found one solution here: http://code.activestate.com/recipes/66064-reading-lines-with-continuation-characters, but it seems unwieldy. There is a nice solution from Daniel Wang in the comments using a generator:
def loglines(rawdata):
lines = []
for i in rawdata.splitlines():
lines.append(i)
if not i.endswith("\\"):
yield "".join(lines)
lines = []
if len(lines)>0: yield "".join(lines)
This works fine, provided you can read the whole file at once. I wondered if there are any built-in functions which handle this, or whether anyone has any other suggestions.
Upvotes: 3
Views: 1851
Reputation: 304137
with open("data.txt") as fin:
for line in fin:
line = line.rstrip('\n')
while line.endswith('\\'):
line = line[:-1] + next(fin).rstrip('\n')
print line
...
You can also pull this out into a generator if you wish
def continuation_lines(fin):
for line in fin:
line = line.rstrip('\n')
while line.endswith('\\'):
line = line[:-1] + next(fin).rstrip('\n')
yield line
with open("long.txt") as fin:
for line in continuation_lines(fin):
...
Upvotes: 14