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

  • SEARCH
  • Home
  • 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 9029921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:21:49+00:00 2026-06-16T07:21:49+00:00

I have a for loop which iterates through a text file (in this case

  • 0

I have a for loop which iterates through a text file (in this case actually a Python file) and it is trying to extract all the functions (looking for the word def). Once it finds that word it starts recording lines until it hits a blank space (which I’m using to denote the end of the function).

My problem is that I want to backup once I hit a def in the file and record any comments that might come before the function. Ex: # This function does the following... etc. I want to backup until I no longer hit a hash.

How would I look backwards with this loop I have written?

for (counter,line) in enumerate(visible_texts):
    line= line.encode('utf-8')
# if line doesn't contain def then ignore it
    if "def" in line and infunction== 0:
        match = re.search(r'\def (\w+)', line.strip())
        line = line.split ("def")[1]    
        print "Recording start of the function..."
        # Backup to see if there's any hashes above it (until the end of the hashes) ** how do I do this **

An example of output I would want at the end would be:

# This function was created by Thomas
# This function print a pass string into the function    
def printme( str ):
       "This prints a passed string into this function"
       print str
       return
  • 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-16T07:21:50+00:00Added an answer on June 16, 2026 at 7:21 am

    Don’t back up; record comments regardless, until you hit another line. If it’s not a def line, discard the comments gathered:

    comments = []
    for (counter, line) in enumerate(visible_texts):
        if line.lstrip().startswith('#'):
            comments.append(line)
        elif "def" in line and not infunction:
            comment = '\n'.join(comments)
            comments = []
            # rest of your code
        else:
            comments = []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a for loop which iterates through currentObject in response. This code: console.log(response[currentObject]);
So I'm trying to loop through all the values of tables, which connect by
I have this code which iterates through ArrayList<String> towns = new ArrayList<String>(); using JSTL
I currently have a loop which iterates through an NSArray of NSString objects. I
In my program I have a while loop which iterates through an array list
I have a loop which basically calls this every few seconds (after the timeout):
During program optimization, trying to optimize a loop that iterates through a vector, I
I have made an each statement which iterates through each class item found. At
I have a loop that iterates through elements in a list. I am required
Working of foreach: As I know, foreach is a loop which iterates through a

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.