read lines without the line break character at the end?

Discussion in 'Python' started by Wai Yip Tung, Sep 2, 2004.

  1. Wai Yip Tung

    Wai Yip Tung Guest

    When I do

    for line in fp:

    the line string usually has a '\n' at the end. In many cases I don't want
    the line break character. I can trim it using

    if line.endswith('\n'): line = line[:-1]

    Given I do it so frequently I wonder if there is some builtin way to say I
    don't wnt the line break character?

    Thanks,

    tung
    Wai Yip Tung, Sep 2, 2004
    #1
    1. Advertising

  2. "Wai Yip Tung" <> writes:

    > When I do
    >
    > for line in fp:
    >
    > the line string usually has a '\n' at the end. In many cases I don't
    > want the line break character. I can trim it using
    >
    > if line.endswith('\n'): line = line[:-1]
    >
    > Given I do it so frequently I wonder if there is some builtin way to
    > say I don't wnt the line break character?


    No. line.rstrip('\n') is a shorter way of trimming the newline
    character, though.

    I guess adding

    def without_newlines(thing):
    for line in thing:
    yield line.rstrip('\n')

    to your snippet collection will do.

    Cheers,
    mwh

    --
    Famous remarks are very seldom quoted correctly.
    -- Simeon Strunsky
    Michael Hudson, Sep 2, 2004
    #2
    1. Advertising

  3. Wai Yip Tung

    Paul McGuire Guest

    "Wai Yip Tung" <> wrote in message
    news:eek:...
    > When I do
    >
    > for line in fp:
    >
    > the line string usually has a '\n' at the end. In many cases I don't want
    > the line break character. I can trim it using
    >
    > if line.endswith('\n'): line = line[:-1]
    >
    > Given I do it so frequently I wonder if there is some builtin way to say I
    > don't wnt the line break character?
    >


    If you have a lot of lines, you will be better off testing the value of
    line[-1] instead of using endswith(). Here's some timeit results:

    line.endswith('\n')
    0.124841844424

    line[-1] == '\n'
    0.057334940614


    -- Paul
    Paul McGuire, Sep 2, 2004
    #3
  4. Wai Yip Tung

    Wai Yip Tung Guest

    Thanks, I like the rstrip() syntax. Coming from Java background. I made
    too many mistakes forgetting to strip the '\n' before using the string.

    > "Wai Yip Tung" <> writes:
    >
    >> When I do
    >>
    >> for line in fp:
    >>
    >> the line string usually has a '\n' at the end. In many cases I don't
    >> want the line break character. I can trim it using
    >>
    >> if line.endswith('\n'): line = line[:-1]
    >>
    >> Given I do it so frequently I wonder if there is some builtin way to
    >> say I don't wnt the line break character?

    >
    > No. line.rstrip('\n') is a shorter way of trimming the newline
    > character, though.
    >
    > I guess adding
    >
    > def without_newlines(thing):
    > for line in thing:
    > yield line.rstrip('\n')
    >
    > to your snippet collection will do.
    >
    > Cheers,
    > mwh
    >
    Wai Yip Tung, Sep 3, 2004
    #4
  5. Wai Yip Tung

    Stan Cook Guest

    try for line in fp:
    whatever line[:-1] etc etc

    Whatever you're doing with the line, the new line character will be gone.


    Stan
    "Wai Yip Tung" <> wrote in message
    news:eek:...
    > When I do
    >
    > for line in fp:
    >
    > the line string usually has a '\n' at the end. In many cases I don't want
    > the line break character. I can trim it using
    >
    > if line.endswith('\n'): line = line[:-1]
    >
    > Given I do it so frequently I wonder if there is some builtin way to say I
    > don't wnt the line break character?
    >
    > Thanks,
    >
    > tung
    Stan Cook, Sep 4, 2004
    #5
  6. Wai Yip Tung

    Andrew Dalke Guest

    Stan Cook wrote:

    > try for line in fp:
    > whatever line[:-1] etc etc
    >
    > Whatever you're doing with the line, the new line character will be gone.


    Except that 1) you should open the file in "U"niversal
    mode, and 2) this will chop the last character from the
    last line in the file if the file doesn't end with
    a newline.

    Andrew
    Andrew Dalke, Sep 4, 2004
    #6
    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. Andreas Leitgeb
    Replies:
    0
    Views:
    438
    Andreas Leitgeb
    May 15, 2009
  2. Mark Space
    Replies:
    0
    Views:
    471
    Mark Space
    May 15, 2009
  3. Lew
    Replies:
    0
    Views:
    931
  4. Joshua Cranmer
    Replies:
    0
    Views:
    451
    Joshua Cranmer
    May 15, 2009
  5. Ciba LO
    Replies:
    4
    Views:
    175
    Ciba LO
    Jul 8, 2006
Loading...

Share This Page