Python Forum
How to capture string from a line to certain line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to capture string from a line to certain line
#1
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()']
}
Reply
#2
the error is here.
(key, val) = line.split()

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 138 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  break print_format lengthy line akbarza 4 273 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,144 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 417 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 270 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 607 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Reading in of line not working? garynewport 2 784 Sep-19-2023, 02:22 PM
Last Post: snippsat
  'answers 2' is not defined on line 27 0814uu 4 668 Sep-02-2023, 11:02 PM
Last Post: 0814uu
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020