Using readline with a new delimiter of line ?

Discussion in 'Python' started by Laurent, Dec 15, 2003.

  1. Laurent

    Laurent Guest

    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
     
    Laurent, Dec 15, 2003
    #1
    1. Advertising

  2. Laurent

    remco Guest

    "Laurent" <> 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
     
    remco, Dec 15, 2003
    #2
    1. Advertising

  3. Laurent

    Laurent Guest

    thanks for this
    Laurent

    "remco" <remco@localhost> a écrit dans le message de news:
    3fddb97c$0$217$4all.nl...
    > "Laurent" <> 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
     
    Laurent, Dec 15, 2003
    #3
  4. Laurent

    Duncan Booth Guest

    "Laurent" <> 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
    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?
     
    Duncan Booth, Dec 15, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phoe6
    Replies:
    3
    Views:
    314
    Bruno Desthuilliers
    Mar 23, 2006
  2. Prasanth
    Replies:
    4
    Views:
    414
    Prasanth
    Nov 22, 2008
  3. gavino
    Replies:
    4
    Views:
    544
    gavino
    Sep 20, 2010
  4. Jean-Michel
    Replies:
    0
    Views:
    368
    Jean-Michel
    Dec 22, 2007
  5. Andrew DeFaria
    Replies:
    1
    Views:
    216
    Ben Morrow
    Jan 30, 2008
Loading...

Share This Page