"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?