[Tutor] Perl to Python code migration
Levy Lazarre
llazarre@yahoo.com
Thu, 5 Sep 2002 09:46:31 -0700 (PDT)
Good afternoon all,
In Python what is the fastest way to break a file into
records according to a fixed string delimiter? I have
a file that looks like this:
(ID 1) IBC NOCHG - Test Code Does Not Exist In STAR
PATIENT CARE...Data Discarded! 0307
PID;00100869^^^A;ZTEST^PATIENT^ONE^^^;19591127;F;221500187^^^A;261-71-0192
FT1|1|20020803.1208|LAB|20020804|20020804|CH|NOCHG|||1|||WLB|||||||||||
(ID 1) IBC 11849 - Test Code Does Not Exist In STAR
PATIENT CARE...Data Discarded! 0354
PID;00026891^^^A;ZTEST^PATIENT^TWO^^^;;;0221500122^^^A;
FT1||409782||20020803133427|20020804|CH|11849|DILTIAZEM
HCL||1|0||WPH|||^EDS^^^|||^^^^^|^Zdog^Shaggy^^^^^|^^^^^^^||^|
The string "(ID 1)" marks the beginning of a new
record. In Perl, I simply do:
local $/ = "(ID 1)"; # change the default input
# record separator
Since Python doesn't have similar constructs, I wonder
what would be the most efficient approach. I do not
want to load the whole file in memory at once.
The purpose is to extract fields from the PID and FT1
segments. I used Perl regular expressions to do this,
but I would like to do it in Python without regexes.
Thanks in advance for any suggestion.
Levy Lazarre
__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com