input record seperator (equivalent of "$|" of perl)

Nick Coghlan ncoghlan at iinet.net.au
Mon Dec 20 11:16:55 CET 2004


les_ander at yahoo.com wrote:
> I would like to read this file as a record.
> I can do this in perl by defining a record seperator;
> is there an equivalent in python? 

Depending on your exact use case, you may also get some mileage out of using the 
csv module with a custom delimeter.

Py> from csv import reader
Py> parsed = reader(demo, delimiter='|')
Py> for line in parsed: print line
...
['a', 'b', 'c', 'd']
['1', '2', '3', '4']

Cheers,
Nick.

P.S. 'demo' was created via:
Py> from tempfile import TemporaryFile
Py> demo = TemporaryFile()
Py> demo.write(txt)
Py> demo.seek(0)
Py> demo.read()
'a|b|c|d\n1|2|3|4'
Py> demo.seek(0)

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list