471,348 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Readline and record separator

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.

Oct 30 '07 #1
7 5017
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.

Oct 30 '07 #2
On 2007-10-30, Johny <py****@hope.czwrote:
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

Oct 30 '07 #3
On Oct 30, 12:21 pm, Johny <pyt...@hope.czwrote:
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)

Oct 30 '07 #4
On Oct 30, 8:21 am, Johny <pyt...@hope.czwrote:
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/Coo.../Recipe/521877.

George

Oct 30 '07 #5
On Oct 30, 7:21 am, Johny <pyt...@hope.czwrote:
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

Oct 30 '07 #6
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).

Oct 30 '07 #7
Dennis Lee Bieber a écrit :
On Wed, 31 Oct 2007 11:13:21 +0100, Bruno Desthuilliers
<br********************@wtf.websiteburo.oops.comde claimed the
following in comp.lang.python:
>[1] Coma Separated Values - but the separator can be almost anything.
Comma...
oops...
though at this time of night I feel like I should be in a
coma <G>
Nov 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Mike Maxwell | last post: by
16 posts views Thread by Douglas | last post: by
3 posts views Thread by Angelic Devil | last post: by
1 post views Thread by Jian Qiu | last post: by
6 posts views Thread by HMS Surprise | last post: by
6 posts views Thread by Lee Sander | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.