[Tutor] Perl to Python code migration

Jeff Shannon jeff@ccvcorp.com
Thu, 05 Sep 2002 11:01:07 -0700


Levy Lazarre wrote:

> Good afternoon all,
>
> In Python what is the fastest way to break a file into
> records according to a fixed string delimiter? [...]
>
> The string "(ID   1)" marks the beginning of a new
> record. [...]

Try using the split() string method.

infile = open('myfile.txt', 'r')
data = infile.read()
infile.close()
records = data.split( "(ID 1)" )

Now 'records' holds a list.  Each element of that list is a block that was
delimited by the argument to split() (in this case, "(ID 1)").  Note that if
your file starts with the delimiter, then records[0] will be an empty string.

You can similarly use split() on each of the records to break those down
further.  Or, you could use find() to locate a particular substring (such as
"PID") and then grab whatever follows that.  You *could* also use Python
regexes to find your fields, though they're probably overkill.  The best
approach here depends a bit on what fields you're actually interested in, and
what demarkation you expect on them.

Jeff Shannon
Technician/Programmer
Credit International