[Tutor] Perl to Python code migration
Erik Price
erikprice@mac.com
Fri, 6 Sep 2002 00:29:45 -0400
On Thursday, September 5, 2002, at 10:24 PM, Jeff Shannon wrote:
>> and you just violated his request that the whole file not be read in
>> at once.
>
> I didn't see any explicit specification along those lines... though
> perhaps it
> was implied by the Perl idiom. I don't know Perl, so I'd miss that.
Surrounded by the question itself and the code, it was pretty easy to
miss, but Levy did say that:
<quote>
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.
</quote>
> Given that restriction, then the best solution probably would be to
> read in small
> chunks of the file (possibly using xreadlines() if it's broken into
> lines) until
> the delimiter is found, and accumulating those chunks manually.
I was thinking about this earlier today, but it's a hard one.
xreadlines() seems like the best solution, except that the data is not
separated into separate lines (it's multi-line). Really the best thing
to do I think would be to write an event handler (like those used in
SAX processing) and parse over the data, setting flags at certain
points and being careful to "grab" the chunks when they are detected.
xreadlines() will work for this as long as the code that does this
parsing is able to "remember" a previous line (i.e. store it in a
temporary variable or something) while it continues to parse the next
lines up to the next delimiter text.
This is one case for XML -- easier to parse than raw data. (Yes I
know, harder to produce than raw data too.)
Erik
--
Erik Price
email: erikprice@mac.com
jabber: erikprice@jabber.org