mac text files & for line

Discussion in 'Python' started by Humberto, Jun 16, 2009.

  1. Humberto

    Humberto Guest

    Greetings.

    This is probably a v. basic question, but my apologies as I'm
    relatively new w/ this.

    But I am attempting to use <i>for line</i> to iterate through a text
    file, but I am working on a Mac and am getting a single block of text.
    I assume this is because of the Mac {CR} usage vs. line feed.

    Is there a programmatic way to use for line to interpret the carriage
    return character as a new line? Otherwise, what are the easiest ways
    to be able to force a replacement of the {CR} character w/ the line
    feed? I've attempted the method using <i>tr</i>, but receive an
    illegal byte sequence error when running the tr '\r' '\n' < file1.txt
    > file2.txt command on my Mac.


    Any help would be greatly appreciated and thanks!
     
    Humberto, Jun 16, 2009
    #1
    1. Advertising

  2. Humberto

    MRAB Guest

    Humberto wrote:
    > Greetings.
    >
    > This is probably a v. basic question, but my apologies as I'm
    > relatively new w/ this.
    >
    > But I am attempting to use <i>for line</i> to iterate through a text
    > file, but I am working on a Mac and am getting a single block of text.
    > I assume this is because of the Mac {CR} usage vs. line feed.
    >
    > Is there a programmatic way to use for line to interpret the carriage
    > return character as a new line? Otherwise, what are the easiest ways
    > to be able to force a replacement of the {CR} character w/ the line
    > feed? I've attempted the method using <i>tr</i>, but receive an
    > illegal byte sequence error when running the tr '\r' '\n' < file1.txt
    >> file2.txt command on my Mac.

    >
    > Any help would be greatly appreciated and thanks!


    Open the file with mode 'U' for universal newline support ('\n', '\r' or
    '\r\n').
     
    MRAB, Jun 16, 2009
    #2
    1. Advertising

  3. Humberto

    Lie Ryan Guest

    Humberto wrote:
    > Greetings.
    >
    > This is probably a v. basic question, but my apologies as I'm
    > relatively new w/ this.
    >
    > But I am attempting to use <i>for line</i> to iterate through a text
    > file, but I am working on a Mac and am getting a single block of text.
    > I assume this is because of the Mac {CR} usage vs. line feed.
    >
    > Is there a programmatic way to use for line to interpret the carriage
    > return character as a new line? Otherwise, what are the easiest ways
    > to be able to force a replacement of the {CR} character w/ the line
    > feed? I've attempted the method using <i>tr</i>, but receive an
    > illegal byte sequence error when running the tr '\r' '\n' < file1.txt
    >> file2.txt command on my Mac.

    >
    > Any help would be greatly appreciated and thanks!


    <psychic_mode>
    I guess this is how you write your code:
    f = open('myfile.txt', 'r').read()
    for line in f:
    print line
    # stream of characters...

    if that's the case, change the code into:
    f = open('myfile.txt', 'r')
    for line in f:
    print line

    If you .read() the file yourself, you'll get a single string of the
    whole file content; '\n' (of whatever real type) inside a string is not
    used as delimiter for a for-loop.
    </psychic_mode>
     
    Lie Ryan, Jun 16, 2009
    #3
  4. Humberto

    Humberto Guest

    On Jun 16, 1:39 pm, MRAB <> wrote:
    > Humberto wrote:
    > > Greetings.

    >
    > > This is probably a v. basic question, but my apologies as I'm
    > > relatively new w/ this.

    >
    > > But I am attempting to use <i>for line</i> to iterate through a text
    > > file, but I am working on a Mac and am getting a single block of text.
    > > I assume this is because of the Mac {CR} usage vs. line feed.

    >
    > > Is there a programmatic way to use for line to interpret the carriage
    > > return character as a new line? Otherwise, what are the easiest ways
    > > to be able to force a replacement of the {CR} character w/ the line
    > > feed? I've attempted the method using <i>tr</i>, but receive an
    > > illegal byte sequence error when running the tr '\r' '\n' < file1.txt
    > >> file2.txt command on my Mac.

    >
    > > Any help would be greatly appreciated and thanks!

    >
    > Open the file with mode 'U' for universal newline support ('\n', '\r' or
    > '\r\n').


    Precisely my problem. Thanks so much. I'd overlooked the references to
    U in the entry for the open function.

    To the other fellow, I think the question was reasonably specific in
    the second paragraph...I sense the helpful response bore that out. I
    certainly acknowledge my mistake in having overlooked the reference in
    the documentation. So my apologies for any inconvenience and thanks
    again.
     
    Humberto, Jun 16, 2009
    #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. Hugo
    Replies:
    10
    Views:
    1,355
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    798
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,199
    Alf P. Steinbach
    May 17, 2009
  4. Marek Stepanek
    Replies:
    12
    Views:
    425
    Peter J. Holzer
    Sep 2, 2006
  5. Replies:
    2
    Views:
    152
Loading...

Share This Page