Parsing by Line Data

Bill Dandreta wjdandreta at att.net
Thu Jun 17 20:32:17 CEST 2004


python1 wrote:
> ...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?

First let me prface my remarks by saying I am not much of a programmer 
so this may not be the best way to solve this but I would use a 
dictionary someting like this (untested):

myinput = open(myfile,'r')
lines = myinput.readlines()
myinput.close()

mydict = {}
index = -1

for l in lines:
   if l[0:2] == '01'
     counter = 0
     index += 1
     mydict[(index,counter)] = l[2:]
   else:
     mydict[(index,counter)] = l[2:]
     counter += 1

You can easy extract the data with a nested loop.

Bill



More information about the Python-list mailing list