Record separator for readlines()

A

Angelic Devil

I know this has been asked before (I already consulted the Google
Groups archive), but I have not seen a definative answer. Is there a
way to change the record separator in readlines()? The documentation
does not mention any way to do this. I know way back in 1998, Guido
said he would consider adding it, but apparently that didn't happen.
Is there some way to do this?
 
J

jepler

I think you still have to roll your own.

Here's a start:
def ireadlines(f, s='\n', bs=4096):
if not s: raise ValueError, "separator must not be empty"
r = []
while 1:
b = f.read(bs)
if not b: break
ofs = 0
while 1:
next = b.find(s, ofs)
if next == -1: break
next += len(s)
yield ''.join(r) + b[ofs:next]
del r[:]
ofs = next
r.append(b[ofs:])
yield ''.join(r)



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDGRQXJd01MZaTXX0RAsZLAJ9g6A4nzcHAnwqUKrn5NL8HxdORZgCeLvLH
dBrgevWmf9PQzqnw3zbD3KA=
=etbR
-----END PGP SIGNATURE-----
 
B

Bengt Richter

--SkvwRMAIpAhPCcCJ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I think you still have to roll your own.

Here's a start:
def ireadlines(f, s='\n', bs=4096):
if not s: raise ValueError, "separator must not be empty"
r = []
while 1:
b = f.read(bs)
if not b: break
ofs = 0
while 1:
next = b.find(s, ofs)
if next == -1: break
next += len(s)
yield ''.join(r) + b[ofs:next]
del r[:]
ofs = next
r.append(b[ofs:])
yield ''.join(r)
What if len(s)>1 and read(bs) reads a partial s?

I posted file splitter some time back which UIGoofed handles that
(still not tested beyond the shown examples, so caveat utor(??) ;-)

http://groups.google.com/group/comp.lang.python/msg/e333f8b2e2fcdc49

Thought I might be missing something, but
... if not s: raise ValueError, "separator must not be empty"
... r = []
... while 1:
... b = f.read(bs)
... if not b: break
... ofs = 0
... while 1:
... next = b.find(s, ofs)
... if next == -1: break
... next += len(s)
... yield ''.join(r) + b[ofs:next]
... del r[:]
... ofs = next
... r.append(b[ofs:])
... yield ''.join(r)
... ...
'123xx678xx' 'Cxx_and so forth' ...
''
oops ...
'123xx' '678xx' 'Cxx' '_and so forth'

Regards,
Bengt Richter
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top