[Tutor] Recording and eliminating continuation lines

Magnus Lycka magnus@thinkware.se
Wed Nov 6 16:35:01 2002


At 08:59 2002-11-06 -0700, Brad Reisfeld wrote:
>I have a file containing a number of lines that I will need to parse.
>Continuation lines in the file are indicated with a backslash at the end of
>the line. I would like to eliminate the continuations, but track which
>'physical lines' lead to each 'logical line'.
>
>For instance, given the input:
>"""This is some text.
>Here is some more text \
>that continues here \
>and here.
>
>This is the final line."""
>
>I'd like this type of output:
>
>[((1,), 'This is some text.'),
>  ((2, 3, 4), 'Here is some more text that continues here and here.'),
>  ((5,), ''),
>  ((6,), 'This is the final line.')]

Do one thing at a time!

 >>> text = r'''first row ends
... second row continued \
... third row continued \
... fourth row ends.
... Fifth row by itself.'''
 >>> rowList = [[]]
 >>> physRow = 1
 >>> for row in text.split('\n'):
...     rowList[-1].append(physRow)
...     physRow += 1
...     if not row.endswith('\\'):
...             rowList.append([])
...
 >>> rowList
[[1], [2, 3, 4], [5], []]
 >>> text = text.replace('\\\n','')
 >>> result = zip(rowList, text.split('\n'))
 >>> import pprint
 >>> pprint.pprint(result)
[([1], 'first row ends'),
  ([2, 3, 4], 'second row continued third row continued fourth row ends.'),
  ([5], 'Fifth row by itself.')]

If you feel it's important you can do a
rowList = map(tuple, rowList)
before the zip.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se