Readline and record separator

J

Johny

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.
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?
Thank you
L.
 
J

Jeff

If it's a short file you could slurp the entire file and then split it
however you like using regular expressions.

I'm not sure if you can alter it, but os.linesp holds the value that
is accessed when file.readlines() splits lines. Conceivably, if it
were set to 'FOO', 'FOO' would be used to determine the EOL string.
 
A

A.T.Hofkamp

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.
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?

Read large enough blocks of data, then use block.find() (in the string module)
to find your delimiter, and split the block.

Albert
 
C

clbr

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.
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?
Thank you
L.

I'm not aware any such a method. But what you could do is simply read
all of them(readlines()) and then split it(delimeter would be your new
line)
 
G

George Sakkis

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.
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?
Thank you
L.

Check out this recipe, it's pretty generic:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/521877.

George
 
B

BartlebyScrivener

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.

Python Cookbook (great book!) 2nd Ed pg. 717: "Iterating on a Stream
of Data Blocks as a Stream of Lines."

Allows you to specify both eol for source and output file.

rd
 
B

Bruno Desthuilliers

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
 
B

Bruno Desthuilliers

Jeff a écrit :
If it's a short file you could slurp the entire file and then split it
however you like using regular expressions.

My my my...
I'm not sure if you can alter it,

You can. But it hopefully won't alter your binary-compiled system libs.
IOW : it's so (obviously) useless that no one even bothered doing
anything to prevent you from altering it !-)

but os.linesp
os.line
holds the value that
is accessed when file.readlines() splits lines. Conceivably, if it
were set to 'FOO', 'FOO' would be used to determine the EOL string.

Hopefully not. Anyway, the answer is elsewhere (hint: import csv).
 
J

Johny

Johny a écrit :


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".


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)" ?
Thanks
L
 
B

Bruno Desthuilliers

Johny a écrit :
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
 
D

Dennis Lee Bieber

Dennis Lee Bieber a écrit :

oops...
Now if I can just recall which newsgroup it was that had a
consistent mis-use of "apart" (It was being used in a sense that really
wanted "a part (of)": <such-and-such> is apart of <this-or-that> should
have been <such-and-such> is a part of <this-or-that>)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top