469,280 Members | 2,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

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 12380
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jocknerd | last post: by
5 posts views Thread by Vai2000 | last post: by
3 posts views Thread by PointMan | last post: by
4 posts views Thread by Phoe6 | last post: by
6 posts views Thread by Sean Davis | last post: by
1 post views Thread by psaffrey | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.