strip newlines and blanks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    strip newlines and blanks

    hi
    i have a file test.dat eg

    abcdefgh
    ijklmn
    <-----newline
    opqrs
    tuvwxyz


    I wish to print the contents of the file such that it appears:
    abcdefgh
    ijklmn
    opqrs
    tuvwxyz

    here is what i did:
    f = open("test.dat" )
    while 1:
    line = f.readline().rs trip("\n")
    if line == '':
    break
    print line

    but it always give me first 2 lines, ie
    abcdefgh
    ijklmn

    What can i do to make it print all w/o the newlines..? and what is the
    proper way to skip printing blank lines while iterating file contents?

    thanks

  • Peter Otten

    #2
    Re: strip newlines and blanks

    micklee74@hotma il.com wrote:

    Mick, you should be a bit more patient. Allow for some time for an answer to
    arrive. Minor edits of your question don't warrant a repost.
    [color=blue]
    > i have a file test.dat eg
    >
    > abcdefgh
    > ijklmn
    > <-----newline
    > opqrs
    > tuvwxyz
    >
    >
    > I wish to print the contents of the file such that it appears:
    > abcdefgh
    > ijklmn
    > opqrs
    > tuvwxyz
    >
    > here is what i did:
    > f = open("test.dat" )
    > while 1:
    > line = f.readline().rs trip("\n")
    > if line == '':
    > break
    > print line
    >
    > but it always give me first 2 lines, ie
    > abcdefgh
    > ijklmn
    >
    > What can i do to make it print all w/o the newlines..? and what is the
    > proper way to skip printing blank lines while iterating file contents?[/color]

    The end of the file is signalled by an empty string, and a blank line with
    the trailing "\n" stripped off is an empty string, too. Therefore you have
    to perform the line == '' test before the stripping.

    Here's an alternative approach:

    for line in f:
    line = line.rstrip()
    if line:
    print line

    This will ignore any lines containing only whitespace and remove trailing
    whitespace from the non-white lines.

    Peter


    Comment

    • Fredrik Lundh

      #3
      Re: strip newlines and blanks

      micklee74@hotma il.com wrote:
      [color=blue]
      > i have a file test.dat eg
      >
      > abcdefgh
      > ijklmn
      > <-----newline
      > opqrs
      > tuvwxyz
      >
      >
      > I wish to print the contents of the file such that it appears:
      > abcdefgh
      > ijklmn
      > opqrs
      > tuvwxyz
      >
      > here is what i did:
      > f = open("test.dat" )
      > while 1:
      > line = f.readline().rs trip("\n")
      > if line == '':
      > break
      > print line
      >
      > but it always give me first 2 lines, ie
      > abcdefgh
      > ijklmn
      >
      > What can i do to make it print all w/o the newlines..? and what is the
      > proper way to skip printing blank lines while iterating file contents?[/color]

      assuming you want to skip *all* blank lines (even if they have whitespace
      on them),

      for line in open("test.dat" ):
      if line.strip():
      print line

      </F>



      Comment

      • micklee74@hotmail.com

        #4
        Re: strip newlines and blanks

        thanks..and sorry, i am using the web version of google groups and
        didn't find an option i can edit my post, so i just removed it..
        thanks again for the reply..

        Comment

        • Sybren Stuvel

          #5
          Re: strip newlines and blanks

          micklee74@hotma il.com enlightened us with:[color=blue]
          > thanks..and sorry, i am using the web version of google groups and
          > didn't find an option i can edit my post[/color]

          It's usenet, you can't edit posts.
          [color=blue]
          > so i just removed it..[/color]

          Which doesn't work at all. Stupid thing they allow you to try and
          delete something on Google Groups, since removal of Usenet posts is
          ignored by most Usetnet carriers.

          Sybren
          --
          The problem with the world is stupidity. Not saying there should be a
          capital punishment for stupidity, but why don't we just take the
          safety labels off of everything and let the problem solve itself?
          Frank Zappa

          Comment

          • Kent Johnson

            #6
            Re: strip newlines and blanks

            micklee74@hotma il.com wrote:[color=blue]
            > hi
            > i have a file test.dat eg
            >
            > abcdefgh
            > ijklmn
            > <-----newline
            > opqrs
            > tuvwxyz
            >
            >
            > I wish to print the contents of the file such that it appears:
            > abcdefgh
            > ijklmn
            > opqrs
            > tuvwxyz
            >
            > here is what i did:
            > f = open("test.dat" )
            > while 1:
            > line = f.readline().rs trip("\n")
            > if line == '':
            > break[/color]

            break terminates the loop, so no more lines will be processed. Use
            continue, which ends only the current iteration of the loop. (Though you
            will need a separate test to terminate the loop when there are no more
            lines.)

            You can iterate an open file directly; here is a shorter version:

            for line in open('test.dat' ):
            line = line.rstrip('\n ')
            if line:
            print line

            Kent
            [color=blue]
            > print line
            >
            > but it always give me first 2 lines, ie
            > abcdefgh
            > ijklmn
            >
            > What can i do to make it print all w/o the newlines..? and what is the
            > proper way to skip printing blank lines while iterating file contents?
            >
            > thanks
            >[/color]

            Comment

            Working...