'While' question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Keshet

    'While' question

    Hi -

    I am writing now my second script ever in python and need some help with
    'while'. I am reading text from a set of files and manipulating the data
    somehow. I use 'while 'word' not in line' to recognize words in the
    texts. Sometimes, the files are empty, so while doesn't find 'word' and
    runs forever. I have two questions:
    1) how do I overcome this, and make the script skip the empty files?
    (should I use another command?)
    2) how do I interrupt the code without closing Python? (I have ActivePython)

    I do know that the strings I am searching for are within the first say
    50 lines.

    Thanks!

    Code:
    |while 'PRIMARY' not in line:
    line = f.readline()[:-1]
    # copy scores
    while 'es' not in line:
    line = f.readline()[:-1]
    out_file.write( line)
    out_file.write( ' ')
    print
    out_file.write( '\n')
    f.close()
    out_file.close( )

    For example, 'PRIMARY' and 'es' do not exist when the file I am reading
    (f) is empty.
    |


  • Wojtek Walczak

    #2
    Re: 'While' question

    On Thu, 21 Aug 2008 18:01:25 -0400, Ben Keshet wrote:
    somehow. I use 'while 'word' not in line' to recognize words in the
    texts. Sometimes, the files are empty, so while doesn't find 'word' and
    runs forever. I have two questions:
    1) how do I overcome this, and make the script skip the empty files?
    (should I use another command?)
    2) how do I interrupt the code without closing Python? (I have ActivePython)
    Try the docs first. You need to read about 'continue' and
    'break' statements: http://docs.python.org/tut/node6.html

    HTH.

    --
    Regards,
    Wojtek Walczak,

    Comment

    • Ben Keshet

      #3
      Re: 'While' question

      Thanks for the reference. I tried it with a general example and got it
      to work - I used an index that counts up to a threshold that is set to
      break. It does not work though with my real code. I suspect this is
      because I cannot really read any lines from an empty file, so the code
      gets stuck even before I get to j=j+1:

      line = f.readline()[:-1]
      j=0
      while 'PRIMARY' not in line:
      line = f.readline()[:-1]
      j=j+1
      if j==30:
      break

      Any suggestions?

      BK


      Wojtek Walczak wrote:
      On Thu, 21 Aug 2008 18:01:25 -0400, Ben Keshet wrote:
      >
      >somehow. I use 'while 'word' not in line' to recognize words in the
      >texts. Sometimes, the files are empty, so while doesn't find 'word' and
      >runs forever. I have two questions:
      >1) how do I overcome this, and make the script skip the empty files?
      >(should I use another command?)
      >2) how do I interrupt the code without closing Python? (I have ActivePython)
      >>
      >
      Try the docs first. You need to read about 'continue' and
      'break' statements: http://docs.python.org/tut/node6.html
      >
      HTH.
      >
      >

      Comment

      • Larry Bates

        #4
        Re: 'While' question

        Ben Keshet wrote:
        Thanks for the reference. I tried it with a general example and got it
        to work - I used an index that counts up to a threshold that is set to
        break. It does not work though with my real code. I suspect this is
        because I cannot really read any lines from an empty file, so the code
        gets stuck even before I get to j=j+1:
        >
        line = f.readline()[:-1]
        j=0
        while 'PRIMARY' not in line:
        line = f.readline()[:-1]
        j=j+1 if j==30:
        break
        Any suggestions?
        >
        BK
        >
        >
        Wojtek Walczak wrote:
        >On Thu, 21 Aug 2008 18:01:25 -0400, Ben Keshet wrote:
        >>
        >>somehow. I use 'while 'word' not in line' to recognize words in the
        >>texts. Sometimes, the files are empty, so while doesn't find 'word'
        >>and runs forever. I have two questions:
        >>1) how do I overcome this, and make the script skip the empty files?
        >>(should I use another command?)
        >>2) how do I interrupt the code without closing Python? (I have
        >>ActivePytho n)
        >>>
        >>
        >Try the docs first. You need to read about 'continue' and
        >'break' statements: http://docs.python.org/tut/node6.html
        >>
        >HTH.
        >>
        >>
        >
        You might consider turning this around into something like:


        for j, line in enumerate(f):
        if 'PRIMARY' in line:
        continue

        if j == 30:
        break



        IMHO this is MUCH easier to understand.

        -Larry

        Comment

        • John Machin

          #5
          Re: 'While' question

          On Aug 22, 9:01 am, Ben Keshet <kesh...@umbc.e duwrote:
          Thanks for the reference. I tried it with a general example and got it
          to work - I used an index that counts up to a threshold that is set to
          break. It does not work though with my real code. I suspect this is
          because I cannot really read any lines from an empty file, so the code
          gets stuck even before I get to j=j+1:
          >
          line = f.readline()[:-1]
          j=0
          while 'PRIMARY' not in line:
          line = f.readline()[:-1]
          j=j+1
          if j==30:
          break
          >
          Any suggestions?
          >
          (1) don't top-post
          (2) use a 'for' statement
          (3) readline is antique
          (4) don't throw away the last character in the line without knowing
          what it is

          for line in f:
          line = line.rstrip('\n ')
          # do something useful here
          if 'PRIMARY' in line:
          break
          # do more useful stuff here

          A quick rule of thumb for Python: if your code looks ugly or strained
          or awkward, it's probably also wrong.

          HTH,
          John

          Comment

          • Bruno Desthuilliers

            #6
            Re: 'While' question

            John Machin a écrit :
            (snip)
            A quick rule of thumb for Python: if your code looks ugly or strained
            or awkward, it's probably also wrong.
            +1 QOTW

            Comment

            • Ben Finney

              #7
              Re: 'While' question

              Bruno Desthuilliers <bruno.42.desth uilliers@websit eburo.invalidwr ites:
              John Machin a écrit :
              (snip)
              A quick rule of thumb for Python: if your code looks ugly or
              strained or awkward, it's probably also wrong.
              >
              +1 QOTW
              Merely a special case of the truism that "Your code is probably wrong
              (regardless of any other properties it may show)".

              That doesn't make John's quote any less worthy of QOTW, though :-)

              --
              \ “The problem with television is that the people must sit and |
              `\ keep their eyes glued on a screen: the average American family |
              _o__) hasn't time for it.” —_The New York Times_, 1939 |
              Ben Finney

              Comment

              Working...