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 do next line output from CSV column? atomxkai 2 419 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  How to do line continuation in Jupyter Notebook? Mark17 4 313 Sep-22-2021, 04:22 PM
Last Post: ibreeden
  append a string to a modified line Mr_Blue 10 570 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 484 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Line 42 syntax error..Help!!1 patpython 4 365 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  [SOLVED] Why does regex fail cleaning line? Winfried 5 569 Aug-22-2021, 06:59 PM
Last Post: Winfried
  Presenting multiline data into single line aaronbuhu 1 358 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  Accessing varying command line arguements Rakshan 3 424 Jul-28-2021, 03:18 PM
Last Post: snippsat
  [Solved] Reading every nth line into a column from txt file Laplace12 7 942 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  [solved] unexpected character after line continuation character paul18fr 4 606 Jun-22-2021, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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