Re: Simple text parsing gets difficult when line continues to next line
- From: "John Machin" <sjmachin@xxxxxxxxxxx>
- Date: 28 Nov 2006 13:29:16 -0800
Tim Hochberg wrote:
[snip]
I agree that mixing the line assembly and parsing is probably a mistake[snip]
although using next explicitly is fine as long as your careful with it.
For instance, I would be wary to use the mixed for-loop, next strategy
that some of the previous posts suggested. Here's a different,
generator-based implementation of the same idea that, for better or for
worse is considerably less verbose:
Here's a somewhat less verbose version of the state machine gadget.
def continue_join_3(linesin):
linesout = []
buff = ""
pending = 0
for line in linesin:
# remove *all* trailing whitespace
line = line.rstrip()
if line.endswith('_'):
buff += line[:-1]
pending = 1
else:
linesout.append(buff + line)
buff = ""
pending = 0
if pending:
raise ValueError("last line is continued: %r" % line)
return linesout
FWIW, it works all the way back to Python 2.1
Cheers,
John,
.
- References:
- Simple text parsing gets difficult when line continues to next line
- From: Jacob Rael
- Re: Simple text parsing gets difficult when line continues to next line
- From: John Machin
- Re: Simple text parsing gets difficult when line continues to next line
- From: Tim Hochberg
- Simple text parsing gets difficult when line continues to next line
- Prev by Date: Re: SPE (Stani's Python Editor) web site?
- Next by Date: Re: How to increase the speed of this program?
- Previous by thread: Re: Simple text parsing gets difficult when line continues to next line
- Next by thread: Re: Simple text parsing gets difficult when line continues to next line
- Index(es):
Relevant Pages
|