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 3 2454
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
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)
iD8DBQFDGRQXJd0 1MZaTXX0RAsZLAJ 9g6A4nzcHAnwqUK rn5NL8HxdORZgCe LvLH
dBrgevWmf9PQzqn w3zbD3KA=
=etbR
-----END PGP SIGNATURE-----
On Fri, 2 Sep 2005 22:10:18 -0500, je****@unpython ic.net wrote: --SkvwRMAIpAhPCcC J 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('123xx678xx Cxx_and so forth') for s in ireadlines(f,'x x',4): print repr(s),
...
'123xx678xx' 'Cxx_and so forth' for s in ireadlines(f,'x x',5): print repr(s),
...
''
oops f.seek(0) for s in ireadlines(f,'x x',5): print repr(s),
...
'123xx' '678xx' 'Cxx' '_and so forth'
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
#
|
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 the error message:
|
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:
BattID VehicleID STDATE STTIME CTC
LKO500HF 00000000 10/27/2003 4:13:51 AM 4
LKO500HF 00000000 10/27/2003 5:13:51 AM 5
LKO500HF 00000000 10/27/2003 10:13:51 AM 6
LKO500HF 00000000 10/27/2003 11:13:51 AM 4
|
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 happens if the number of lines is huge? I have
a very big file (4GB) I want to
read in, but I'm sure there must be some limitation to readlines and I'd
like to know how it is handled by python.
I am using it like this:
|
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 long record (10
records are lump into one record). Thank you in
advance for your help. Here is the code:
****************************************
infile = open('c:/grad3650txt.csv', 'r')
| |
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 to using
version 2.2. That being the case what is a good standard record
separator to use to ensure that I read in one record (list of lists)
at a time, '\n'? I want to try to stay with what is considered
standard python idioms.
Thanks,
|
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 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.
|
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) running a basic Java
program to count lines yields a number in that range.
However, when I use Python's various methods -- readline(),
readlines(), or xreadlines() and loop through the lines of the file,
the line program exits at 16,000 lines. No...
|
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 those sensors. Based on
the "commands", the sensors keep sending corresponding data back to
the instrument which wraps up those data and put into "com1" . The
readlines() of pyserial pick up those data for processing.
The data ’string' does...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |