473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Record separator for readlines()


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?

--
"First they ignore you, then they laugh at you, then they fight you,
then you win."
-- Mohandas Gandhi
Sep 2 '05 #1
3 2432
universal newlines?
http://www.python.org/doc/2.3.3/whatsnew/node7.html

Angelic Devil wrote:
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?

--
"First they ignore you, then they laugh at you, then they fight you,
then you win."
-- Mohandas Gandhi


Sep 2 '05 #2
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)

iD8DBQFDGRQXJd01MZaTXX0RAsZLAJ9g6A4nzcHAnwqUKrn5NL 8HxdORZgCeLvLH
dBrgevWmf9PQzqnw3zbD3KA=
=etbR
-----END PGP SIGNATURE-----

Sep 3 '05 #3
On Fri, 2 Sep 2005 22:10:18 -0500, je****@unpythonic.net wrote:

--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....33f8b2e2fcdc49

Thought I might be missing something, but
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)
... from StringIO import StringIO as SIO
f = SIO('123xx678xxCxx_and so forth')
for s in ireadlines(f,'xx',4): print repr(s), ...
'123xx678xx' 'Cxx_and so forth' for s in ireadlines(f,'xx',5): print repr(s), ...
''
oops f.seek(0)
for s in ireadlines(f,'xx',5): print repr(s),

...
'123xx' '678xx' 'Cxx' '_and so forth'

Regards,
Bengt Richter
Sep 3 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: les_ander | last post by:
Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are name profession id #
9
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get...
8
by: mark | last post by:
Access2000 How do I write a query that combines the CTC field from each record below into one record? I need to concatenate the CTC field with a separator, like below: ...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
2
by: Eduardo Biano | last post by:
I am a python newbie and I have a problem with writing each record read to a file. The expected output is 10 rows of records, but the actual output of the code below is only one row with a very...
6
by: HMS Surprise | last post by:
I need to write 2 member lists to a file. For each record the number of these lists can be different. I think a good way to handle that may be to make each record a list of lists. I am restricted...
7
by: Johny | last post by:
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...
7
by: Wojciech Gryc | last post by:
Hi, I'm currently using Python to deal with a fairly large text file (800 MB), which I know has about 85,000 lines of text. I can confirm this because (1) I built the file myself, and (2)...
5
by: zxo102 | last post by:
Hello All, I have a system. An instrument attched to 'com1' is wireless connected to many sensors at different locations. The instrument can forward the "commands" (from pyserial's write()) to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.