472,354 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 13977
"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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.