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
  No new line from print in a browser Lou 13 1,366 Dec-01-2025, 06:39 PM
Last Post: noisefloor
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 931 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,937 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
  I am a newbie I like to use the command line Mikel2025 1 1,063 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,237 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 967 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 1,940 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 3,184 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 3,595 Sep-09-2024, 08:03 AM
Last Post: pinkang

Forum Jump:

User Panel Messages

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