parsing a dbIII file

Jay Loden python at jayloden.com
Tue Aug 7 10:46:25 EDT 2007


korovev76 at gmail.com wrote:
> Hello everybody, I'm new to python (...I work with cobol...)
> 
> I have to parse a file (that is a dbIII file) whose stucture look like
> this:
> |string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
> other string|

There are a number of relatively simple options that come to mind, including
regular expressions:

##### BEGIN CODE #####

import re

#
# dbIII.txt:
# |string|, |string|, |string|, |string|, |,1,2,3,4|, |other string|
#
handle = open('dbIII.txt')
for line in handle.xreadlines():
    for match in re.finditer(r'\|\s*([^|]+)\s*\|,*', line):
        for each in match.groups():
            print each

handle.close()

##### END CODE #####


Without knowing what you need to do with the data, it's hard to suggest a better
method for parsing it. The above should work, provided that the data is always
in the format | data | with no pipe symbols in between the ones used as separators.

HTH,

-Jay



More information about the Python-list mailing list