stripping blanks

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

    #1

    stripping blanks

    hi
    i have a file test.dat eg

    abcdefgh
    ijklmn
    <-----newline
    opqrs
    tuvwxyz
    <---newline

    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
    #if not re.findall(r'^$ ',line):
    print line

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

    What can i do to make it print all..?
    thanks

  • seeker

    #2
    Re: stripping blanks

    micklee74@hotma il.com wrote:[color=blue]
    > hi
    > i have a file test.dat eg
    >
    > abcdefgh
    > ijklmn
    > <-----newline
    > opqrs
    > tuvwxyz
    > <---newline
    >
    > 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
    > #if not re.findall(r'^$ ',line):
    > print line
    >
    > but it always give me first 2 lines, ie
    > abcdefgh
    > ijklmn
    >
    > What can i do to make it print all..?
    > thanks
    >[/color]
    Hi,

    this should work better:


    f = open("test.dat" )
    while 1:
    line = f.readline().rs trip("\n")
    if line == '':
    continue
    #if not re.findall(r'^$ ',line):
    print line

    Comment

    • seeker

      #3
      Re: stripping blanks

      micklee74@hotma il.com wrote:[color=blue]
      > hi
      > i have a file test.dat eg
      >
      > abcdefgh
      > ijklmn
      > <-----newline
      > opqrs
      > tuvwxyz
      > <---newline
      >
      > 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
      > #if not re.findall(r'^$ ',line):
      > print line
      >
      > but it always give me first 2 lines, ie
      > abcdefgh
      > ijklmn
      >
      > What can i do to make it print all..?
      > thanks
      >[/color]

      Last suggestion I made was bad. Here is the new one. Hopefully thats
      correct.

      f = open("test.dat" )
      for line in f:
      printLine = line.rstrip("\n ")
      if not printLine:
      continue
      print printLine

      Comment

      • Fulvio

        #4
        Re: stripping blanks

        Alle 17:06, martedì 02 maggio 2006, seeker ha scritto:[color=blue]
        >      printLine = line.rstrip("\n ")[/color]

        I think that nobody considered if the text has (\r) or (\r\n) or (\n) at the
        end of the line(s).

        Comment

        • Duncan Booth

          #5
          Re: stripping blanks

          Fulvio wrote:
          [color=blue]
          > Alle 17:06, martedì 02 maggio 2006, seeker ha scritto:[color=green]
          >>      printLine = line.rstrip("\n ")[/color]
          >
          > I think that nobody considered if the text has (\r) or (\r\n) or (\n)
          > at the end of the line(s).
          >[/color]
          You think wrongly.

          The suggested code opens the file in text mode so the line endings are
          automatically normalised to '\n'.

          Comment

          • Fredrik Lundh

            #6
            Re: stripping blanks

            Fulvio wrote:
            [color=blue][color=green]
            > > printLine = line.rstrip("\n ")[/color]
            >
            > I think that nobody considered if the text has (\r) or (\r\n) or (\n) at the
            > end of the line(s).[/color]

            if it's opened in text mode (the default), and is a text file, it will
            always have "\n" at the end of the line.

            </F>



            Comment

            • Scott David Daniels

              #7
              Re: stripping blanks

              seeker wrote:
              [color=blue]
              > Last suggestion I made was bad. Here is the new one. Hopefully thats
              > correct.
              >
              > f = open("test.dat" )
              > for line in f:
              > printLine = line.rstrip("\n ")
              > if not printLine:
              > continue
              > print printLine[/color]

              A little better:

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

              --Scott David Daniels
              scott.daniels@a cm.org

              Comment

              • Edward Elliott

                #8
                Re: stripping blanks

                Scott David Daniels wrote:[color=blue]
                > A little better:
                >
                > f = open("test.dat" )
                > for line in f:
                > printLine = line.rstrip("\n ")
                > if printLine:
                > print printLine[/color]

                [sys.stdout.writ e(line) for line in open('test.dat' ) if line.rstrip('\n ')]

                Where's my prize? What do you mean, shorter isn't always better? ;)

                Comment

                Working...