How to capture string from a line to certain line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to capture string from a line to certain line (/thread-34135.html) |
How to capture string from a line to certain line - jerald - Jun-30-2021 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: 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()'] } RE: How to capture string from a line to certain line - Larz60+ - Jun-30-2021 the error is here. (key, val) = line.split() split returns a list, which is one object. You're trying to read two items. |