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
  Βad Input on line 12 Azdaghost 5 1,287 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 277 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 1,018 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 822 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 972 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 2,442 Sep-09-2024, 08:03 AM
Last Post: pinkang
  Simplest way to run external command line app with parameters? Winfried 2 1,277 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Printing the code line number arbiel 6 1,654 Jun-30-2024, 08:01 AM
Last Post: arbiel
  How to add multi-line comment section? Winfried 2 1,373 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 2,010 May-23-2024, 03:02 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