473,387 Members | 3,781 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,387 software developers and data experts.

Using readline with a new delimiter of line ?

Hello,

is it possible to read a file in python line by line
by redefining a new end-of-line delimiter ?

I would like for example to have the string "END" being the new delimiter
for each line.

Thanks
Jul 18 '05 #1
3 14345
"Laurent" <La************@wanadoo.fr> wrote:
is it possible to read a file in python line by line
by redefining a new end-of-line delimiter ?

I would like for example to have the string "END" being the new delimiter
for each line.


i couldn't find any builtin option. But of course you could always use:
listOfLines = file('<filename>','r').read().split('END')

Cheers!
remco
Jul 18 '05 #2
thanks for this
Laurent

"remco" <remco@localhost> a écrit dans le message de news:
3f*********************@news.xs4all.nl...
"Laurent" <La************@wanadoo.fr> wrote:
is it possible to read a file in python line by line
by redefining a new end-of-line delimiter ?

I would like for example to have the string "END" being the new delimiter
for each line.


i couldn't find any builtin option. But of course you could always use:
listOfLines = file('<filename>','r').read().split('END')

Cheers!
remco

Jul 18 '05 #3
"Laurent" <La************@wanadoo.fr> wrote in news:brkd27$6es$1@news-
reader4.wanadoo.fr:
Hello,

is it possible to read a file in python line by line
by redefining a new end-of-line delimiter ?

I would like for example to have the string "END" being the new delimiter
for each line.

If your file is short enough that it will fit comfortably into memory, just
do:

inputFile = file('name', 'rb')
lines = inputFile.read().split('END')

then you can iterate over the list stored in 'lines'.

If the file is too large for that, you'll need to implement your own scheme
to read chunks, split them up on your delimiter and return a line at a
time. The easiest way to do that is to use a generator. Something like:

def oddFile(filename, delimiter, buffersize=10000):
inputFile = file(filename, 'rb')
lines = ['']
for data in iter(lambda: inputFile.read(buffersize), ''):
lines = (lines[-1] + data).split(delimiter)
for line in lines[:-1]:
yield line
yield lines[-1]

Then you can do:

for l in oddFile('somefile', 'END'):
print repr(l)

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #4

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

Similar topics

3
by: peter leonard | last post by:
Hi, I having a problem with reading each line from a text file. For example, the file is a text file named 'test.txt' with the following content : line 1 line 2 line 3 line 4 line 5
6
by: Jocknerd | last post by:
I'm a Python newbie and I'm having trouble with Regular Expressions when reading in a text file. Here is a sample layout of the input file: 09/04/2004 Virginia 44 Temple ...
5
by: Vai2000 | last post by:
Hi All, back in VC++ we had flexibility of reading the line defined by our own terminator... get(buf,sizeof(buf),chTerminator); Is there something similar in C#... My file is not terminated by...
2
by: Mattyw | last post by:
Hi I have a sqlcommand that returns all the rows in a column and then pass that to a datareader. I am new to VS.Net and so far I can only return the first row in the first column using ...
7
by: Mike Joseph | last post by:
I have a data file that was produced in MicroSoft Access. It's a table that was exported as a fixed format, sequential access ASCII file. It contains over 80,000 records. Basically, all the stats...
3
by: PointMan | last post by:
what i know is... while ((currentStr = sr.ReadLine()) != null) { in this area, can i get next ReadLine Value of currentStr } best regard,,
4
by: Phoe6 | last post by:
Hi, I have a configfile, in fact, I am providing a configfile in the format: Name: Foo Author: Bar Testcases: tct123
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
1
by: psaffrey | last post by:
I'm trying to implement an application that will listen for requests, run them when they are present but also be able to add new requests even while it's running. I've tried to do this using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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.