Readline and record separator

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Oct 31 06:13:21 EDT 2007


Johny a écrit :
> On Oct 30, 8:44 pm, Bruno Desthuilliers
> <bdesth.quelquech... at free.quelquepart.fr> wrote:
>> Johny a écrit :
>>
>>> Is it possible to change record separator when using readline?
>>> As far as I know readline reads characters until found '\n' and it is
>>> the end of record for readline.
>> This is not a "record" separator, but a newline. As the name implies,
>> file.readline is about reading a text file line by line. For a
>> definition of "line" being : "a chuk of text that starts either at the
>> beginning of the document or after a newline" and for a definition of
>> "newline" being "a platform-specific character or character sequence".
>>
>>> My problem is that my record consits several '\n' and when I use
>>> readline it does NOT read the whole my record.
>>> So If I could change '\n' as  a record separator for readline, it
>>> would solve my problem.
>>> Any idea?
>> If you're dealing with (so-called) CSV files, you might want to have a
>> look at the (oh surprise) CSV module (in the stdlib) instead.
>>
>> HTH
> 
> Bruno,
> Thank you for your hint, but can you please be more specific when
> saying
> "look at the (oh surprise) CSV module (in the stdlib)" ?

You used the term "record" instead of "line", which is to me a clear 
indication that you're not using the appropriate tools here. If your 
file is a (so-called) CSV[1] file, there's a module for this (named 
'csv') in the standard lib.

And even if your file format is not CSV-compatible, there may be some 
answers to your problem in this module's source code (hint : one of 
these answers is "don't use line-oriented APIs when not appropriate").


[1] Coma Separated Values - but the separator can be almost anything.

HTH



More information about the Python-list mailing list