Parsing by Line Data

Gustavo Picon gpicon at gmail.com
Thu Jun 17 22:22:12 CEST 2004


Quick and dirty solutions, with no error checking:

# method 1: list of strings
i = -1
r = []
for l in file(somefile):
    if l.startswith('01'):
        i+= 1
        r.append(l[2:].rstrip())
    else:
        r[i] = '%s%s' % (r[i], l[2:].rstrip())
print r

# method 2: list of lists
i = -1
r = []
for l in file(somefile):
    if l.startswith('01'):
        i+= 1
        r.append([l[2:].rstrip()])
    else:
        r[i].append(l[2:].rstrip())
print r

Regards.

On Thu, 17 Jun 2004 10:17:58 -0700, python1 <python1 at spamless.net> wrote:
> 
> Having slight trouble conceptualizing a way to write this script. The
> problem is that I have a bunch of lines in a file, for example:
> 
> 01A\n
> 02B\n
> 01A\n
> 02B\n
> 02C\n
> 01A\n
> 02B\n
> .
> .
> .
> 
> The lines beginning with '01' are the 'header' records, whereas the
> lines beginning with '02' are detail. There can be several detail lines
> to a header.
> 
> I'm looking for a way to put the '01' and subsequent '02' line data into
> one list, and breaking into another list when the next '01' record is found.
> 
> How would you do this? I'm used to using 'readlines()' to pull the file
> data line by line, but in this case, determining the break-point will
> need to be done by reading the '01' from the line ahead. Would you need
> to read the whole file into a string and use a regex to break where a
> '\n01' is found?
> --
> http://mail.python.org/mailman/listinfo/python-list
> 




-- 
Gustavo Picon
http://slashcore.com/tabo/




More information about the Python-list mailing list