Python Forum

Full Version: How to capture string from a line to certain line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a file called abc.py with has several function which is the code below. I have another python file to extract abc.py as string.
GOAL: how to capture the start line def hello_step0() until the login() and store it as dictionary and then continue to capture the second and third. I want to capture each function and the code consist inside the function.
What can i do to achieve the expected output. Thanks
#abc.py
def hello_step0():

	element_wait_accid_click(self, ccc, ssss, accid, timeout=30)
	creation(self, driver)
	login()

def hello2_step1():
	delete()

def hello2_step2():
	delete()
what I tried:

#!C:\Python\Python39\python.exe
print ('Content-type: text/html\n\n')
d = {}
with open("abc.py") as f:
    for line in f:
     (key, val) = line.split()
     d[int(key)] = val

print (d)
OUTPUT:
Error:
Traceback (most recent call last): File "c:\xampp\htdocs\project_real\ignore.py", line 32, in <module> (key, val) = line.split() ValueError: not enough values to unpack (expected 2, got 1)
EXPECTED OUTPUT:
thisdict = {
  'def hello_step0()': ['element_wait_accid_click(self, ccc, ssss, accid, timeout=30),creation(self, driver),login()'],
  'def hello2_step1():':['delete()'],
  'def hello2_step2():': ['delete()']
}
the error is here.
(key, val) = line.split()

split returns a list, which is one object. You're trying to read two items.