stripping

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

    #1

    stripping

    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

  • Edward Elliott

    #2
    Re: stripping

    micklee74@hotma il.com wrote:[color=blue]
    > while 1:
    > line = f.readline().rs trip("\n")
    > if line == '':
    > break
    > #if not re.findall(r'^$ ',line):
    > print line[/color]

    you want continue, not break there. but that gives you an infinite loop, so
    you need a new exit condition. you're better off scrapping the while and
    using a for loop or list comprehension on readline.

    Comment

    • Jesse Hager

      #3
      Re: stripping

      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]
      Change the 'break' statement to a 'continue'.

      --
      Jesse Hager
      email = "wrffruntre@tzn vy.pbz".decode( "rot13")

      Comment

      Working...