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 6844657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:24:18+00:00 2026-05-27T00:24:18+00:00

first of all, thank you for your help in forward. I’m using Python and

  • 0

first of all, thank you for your help in forward.
I’m using Python and I’m trying to search a .py file for all of its functions starting with the name “test_” and all of the variables included. The variables I search for are formatted like this: “var[“blabla”]”. So here I have an example:

def test_123:

    init = var["blabla1"]
    init2 = var["blabla2"]
    *somecode*

def test_456:

    init3 = var["blabla3"]
    init4 = var["blabla4"]
    *somecode*

What I already wrote is a script, that returns all my functions and variables in a html file. But I have to get them sorted, so I can work with them better.

Right now its like this:

test_123,test456
var["blabla1"],var["blabla2"],...

And I want it like this:

test_123:

var["blabla1"]
var["blabla2"]

test_456:

var["blabla3"]
var["blabla4"]

EDIT: I have this right now:

def suchentpar():
    fobj = open("2.py", "r")
    search = fobj.read()
    tpar = re.findall(r'var\[\"\w+\"\]',search)
    return tpar
    fobj.close()

def suchenseq():
    fobj = open("2.py", "r")
    search = fobj.read()
    seq = re.findall(r'test\_\w+',search)
    return seq
    fobj.close()
  • 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-05-27T00:24:19+00:00Added an answer on May 27, 2026 at 12:24 am

    First off, your code as is will never run fobj.close(), given that the functions will exit via return the line above…

    Then, a way to obtain what you want could be:

    import re
    
    fcontent = '''
    def test_a(self):
        var["hello"]
        var["world"]
    
    def test_b(self):
        var["hola"]
        var["mundo"]
    '''
    
    dict_ = {}
    chunks = [chunk for chunk in fcontent.split('def ') if chunk.strip()]
    for chunk in chunks:
        tname = re.findall(r'test\_\w+', chunk)[0]
        vars = re.findall(r'var\[\"\w+\"\]', chunk)
        dict_[tname] = vars
    for k, v in dict_.items():
        print k
        for e in v:
            print "\t%s" % e
    

    NOTE: In the above code I left the regexes as you wrote them, but of course you could improve on them, and could change the first re.findall in re.search if you wished. In other words: what above is purely a demo to show the concept, but you should work on the edge cases and efficiency…

    HTH!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, thank you all for your help. I'm trying to locate something similar to
Hi first I like to thank you all for your help We have to
First, I would to thank everyone for all the help they provide via this
First of all, I have to thank StackOverflow and all its members for the
EDIT: Thank you all for your help. I edited my Database class to contain
first of all thanks for taking your time! I'm a junior Dev, working with
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
First, thanks for all the help I've received so far from StackOverflow. I've learned
First all, I am still learning CakePHP and I am close to ZERO using
Thank you in advance for your help. Background: I have two TableViews (NSArray datasource)

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.