By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

Readline and record separator

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.