Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9167517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:24:36+00:00 2026-06-17T15:24:36+00:00

Hey I am a newbie at python and I need some help. I’ve written

  • 0

Hey I am a newbie at python and I need some help. I’ve written down the following code:

 try:
  it = iter(cmLines)
  line=it.next()
  while (line):
    if ("INFERNAL1/a" in line) or ("HMMER3/f" in line) :
      title = line
      line = it.next()
      if word2(line) in namesList: //if second word in line is in list
        output.write(title)
        output.write(line)
        line = it.next()
        while ("//" not in line):
          output.write(line)
          line = it.next()
        output.write(line)
    line = it.next()
except Exception as e:
  print "Loop exited becuase:"
  print type(e)
  print "at " + line
finally:
  output.close()
  1. When the loop ends it always throws an Exception that notifies that the loop stopped. Even though it didn’t terminate prematurely. How do I stop that?

  2. Is there a better way to write my code? Something more stylish. I have a big file that has lots of information and I am trying to catch only the information I need. Every slice of information is of the format:

    Infernal1/a ...
    Name someSpecificName
    ...
    ...
    ...
    ...
    // 
    

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:24:37+00:00Added an answer on June 17, 2026 at 3:24 pm

    RocketDonkey’s answer is spot-on. Because of the complexity of the way you’re iterating, there is no simple way to do this with a for loop, so you’re going to need to explicitly handle StopIteration.

    However, if you rethink the problem a bit, there are other ways around this. For example, a trivial state machine:

    try:
        state = 0
        for line in cmLines:
            if state == 0:
                if "INFERNAL1/a" in line or "HMMER3/f" in line:
                    title = line
                    state = 1
            elif state == 1:
                if word2(line) in NamesList:
                    output.write(title)
                    output.write(line)
                    state = 2
                else:
                    state = 0
            elif state == 2:
                output.write(line)
                if '//' in line:
                    state = 0
    except Exception as e:
        print "Loop exited becuase:"
        print type(e)
        print "at " + line
    finally:
        output.close()
    

    Alternatively, you can write a generator function that delegates to sub-generators (via yield from foo() if you’re in 3.3, via for x in foo(): yield x if not), or various other possibilities, especially if you rethink your problem at a higher level.

    That may not be what you want to do here, but it’s usually worth at least thinking about “Can I turn this while loop and two explicit next calls into a for loop?”, even if the answer turns out to be “No, not without making things less readable.”

    As a side note, you can probably simplify things by replacing the try/finally with a with statement. Instead of this:

    output = open('foo', 'w')
    try:
        blah blah
    finally:
        output.close()
    

    You can just do this:

    with open('foo', 'w') as output:
        blah blah
    

    Or, if output isn’t a normal file, you can still replace the last four lines with:

    with contextlib.closing(output):
        blah blah
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, was wondering if anyone could help a newbie on SQL and Python.
Hey All, I am a pointer newbie and in the following code, I am
Hey all Im a newbie Androidian, and would love some help with this? android.database.sqlite.SQLiteException:
Hey everyone I am a newbie and would love some help. I got the
hey, I am a newbie in symfony. I am following this joobet tutorial on
Hey everybody, I am just newbie at Python. I wanted to write a script
Hey, super newbie question. Consider the following WCF function: [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode
Hey all, Newbie here, so please forgive my approach and specifics. I'd appreciate some
Newbie question again... I already have the following code for making links shortened using
Hi I'm a newbie for Android... Please throw some light on the following issue..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.