OO misconceptions
Alex Martelli
aleaxit at yahoo.com
Wed Jul 18 04:08:45 EDT 2001
"Tim Hammerquist" <tim at vegeta.ath.cx> wrote in message
news:slrn9la9o0.vj8.tim at vegeta.ath.cx...
...
> Actually, I have a snippet of code I'd love to be able to convert to
> Python. The only thing is I can't find a Python equivalent for Perl's
> record separator variable ($/). This is from 'Perl Cookbook' (ORA).
Have a look at my recently-posted recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66063
"Read a text file by-paragraph". It may need adaptation (e.g.
if you need to be able to read empty 'paragraphs', use separators
that are not whole lines, &c) but I hope the basic idea is sound.
Maybe something like that might be integrated in a future
version of standard module fileinput...?
If the file you're reading is not TOO huge, and you don't care
about implicit suppression of empty paragraphs (which Perl, I
believe, only supplies when $/ is ''), much simpler solutions
would be possible -- down to the simplistic case of:
def Paragraphs(fileobject, separator='%%\\n'):
return fileobject.read().split(separator)
and of course this is a problem that cries out for generators,
so Python 2.2 should offer better ways to frame solutions.
Alex
More information about the Python-list
mailing list