\r for newline in readlines function

Discussion in 'Python' started by Leeds, Mark, Sep 19, 2003.

  1. Leeds, Mark

    Leeds, Mark Guest

    I did more investigation into my previous problem
    and what happens is that my text file has \r for representing
    a new line instead
    of a \n. is there a way to tell the readlines
    function that the symbol for a newline is \r rather
    than \n ? right now the readlines function reads
    the\r as another item in the list and just puts
    everything into one big list because it
    doesn't find any new line characters.
    thanks. i'm really stuck because
    i can't read my data right now.

    mark
     
    Leeds, Mark, Sep 19, 2003
    #1
    1. Advertising

  2. Leeds, Mark

    Jules Dubois Guest

    On Fri, 19 Sep 2003 12:37:42 -0400, in article
    <>, Leeds, Mark wrote:

    > I did more investigation into my previous problem
    > and what happens is that my text file has \r for representing
    > a new line instead
    > of a \n.


    If you open a file in text mode, you're probably getting text as the
    implementation library or operating system view it.

    Under MS-DOS and derivative system (e.g., Windows), text files are stored
    with CRLF as the line delimiter but are returned to the program without the
    CR. Under MacOS text files use CR as the delimiter, and under Unix, LF.

    > is there a way to tell the readlines
    > function that the symbol for a newline is \r rather
    > than \n ?


    I think this is proper behavior, and the problem lies in the combination of
    file format and the operating system under which you're executing. A
    simple but inefficient solution is the write your own input function to
    read the file one character at a time and handle characters like CR and LF
    in a manner suitable to your requirements.
     
    Jules Dubois, Sep 19, 2003
    #2
    1. Advertising

  3. Leeds, Mark

    Peter Otten Guest

    Leeds, Mark wrote:

    > I did more investigation into my previous problem
    > and what happens is that my text file has \r for representing
    > a new line instead
    > of a \n. is there a way to tell the readlines
    > function that the symbol for a newline is \r rather
    > than \n ? right now the readlines function reads
    > the\r as another item in the list and just puts
    > everything into one big list because it
    > doesn't find any new line characters.
    > thanks. i'm really stuck because
    > i can't read my data right now.
    >
    > mark


    Try opening the file with the "U" universal newlines flag:

    f = file(filename, "rU")
    print f.readlines()

    I think this can cope with all three newline variants regardless of the
    actual platform.

    Peter
     
    Peter Otten, Sep 19, 2003
    #3
  4. In article <>,
    "Leeds, Mark" <> wrote:

    > I did more investigation into my previous problem
    > and what happens is that my text file has \r for representing
    > a new line instead
    > of a \n. is there a way to tell the readlines
    > function that the symbol for a newline is \r rather
    > than \n ? right now the readlines function reads
    > the\r as another item in the list and just puts
    > everything into one big list because it
    > doesn't find any new line characters.
    > thanks. i'm really stuck because
    > i can't read my data right now.


    Isn't this exactly what the new universal newline format is for?
    <http://www.python.org/peps/pep-0278.html>

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
     
    David Eppstein, Sep 19, 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. Skip Montanaro

    RE: \r for newline in readlines function

    Skip Montanaro, Sep 19, 2003, in forum: Python
    Replies:
    0
    Views:
    663
    Skip Montanaro
    Sep 19, 2003
  2. Jorge Godoy
    Replies:
    2
    Views:
    255
    Jorge Godoy
    Oct 14, 2003
  3. Richard
    Replies:
    5
    Views:
    322
    Hari Pulapaka
    Aug 13, 2004
  4. Uwe Mayer

    blocking file.readlines() needed

    Uwe Mayer, Aug 13, 2004, in forum: Python
    Replies:
    2
    Views:
    254
    Peter Otten
    Aug 13, 2004
  5. Yong Wang

    readlines()

    Yong Wang, Aug 18, 2004, in forum: Python
    Replies:
    2
    Views:
    360
    wes weston
    Aug 18, 2004
Loading...

Share This Page