Extract specific variable names from functions
Question:
Below you can see the function for extracting separate words from the text. Below you can see the text and code.
import re
text = '''
def function_cal(revenues_shops, surplus_margin, meadian_profit):
median_profit= revenues_shops* surplus_margin
return median_profit
def function_cal_new (revenues_stories_new, surplus_margin_new, meadian_profit):
median_profit= revenues_stories_new* surplus_margin_new
return median_profit
def function_cal_new1 (revenues_stories_new1, surplus_margin_new1, meadian_profit):
median_profit= revenues_stories_new1* surplus_margin_new1
return median_profit
'''
def extraction_variables(text):
splited_table1, splited_table2 = dict(), dict()
lines = text.split('n')
for line in lines:
x = re.search(r"^def.*:$", line)
if x is not None:
values = x[0].split('def ')[1].split('(')
splited_table1 = values[0]
splited_table2 = values[1][:-2].split(', ')
yield splited_table1, splited_table2
splited_table1, splited_table2 = zip(*extraction_variables(text))
Now I want to put extracted words from splited_table1 and splited_table2 in the same place.
I tried with next line of code:
splited_table1+splited_table2
But after execution of the above line I have results this results:
('function_cal',
'function_cal_new ',
'function_cal_new1 ',
['revenues_shops', 'surplus_margin', 'meadian_profit'],
['revenues_stories_new', 'surplus_margin_new', 'meadian_profit'],
['revenues_stories_new1', 'surplus_margin_new1', 'meadian_profit'])
But instead of having only words separated by commas, I can see a lot of brackets. So can anybody help me how to solve this and to have only words without brackets and quotation marks?
Answers:
If you want to place the words into a data structure you can use the following code (replacing the last line of code in you shared):
splited_table1, splited_table2 = zip(*extraction_variables(text))
table = []
for elem in splited_table1:
table.append(elem)
for sub_array in splited_table2:
for elem in sub_array:
table.append(elem)
print(table)
The complete code looks like this:
import re
text = '''
def function_cal(revenues_shops, surplus_margin, meadian_profit):
median_profit= revenues_shops* surplus_margin
return median_profit
def function_cal_new (revenues_stories_new, surplus_margin_new, meadian_profit):
median_profit= revenues_stories_new* surplus_margin_new
return median_profit
def function_cal_new1 (revenues_stories_new1, surplus_margin_new1, meadian_profit):
median_profit= revenues_stories_new1* surplus_margin_new1
return median_profit
'''
def extraction_variables(text):
splited_table1, splited_table2 = dict(), dict()
lines = text.split('n')
for line in lines:
x = re.search(r"^def.*:$", line)
if x is not None:
values = x[0].split('def ')[1].split('(')
splited_table1 = values[0]
splited_table2 = values[1][:-2].split(', ')
yield splited_table1, splited_table2
splited_table1, splited_table2 = zip(*extraction_variables(text))
table = []
for elem in splited_table1:
table.append(elem)
for sub_array in splited_table2:
for elem in sub_array:
table.append(elem)
print(table)
And the output looks like this:
['function_cal', 'function_cal_new ', 'function_cal_new1 ', 'revenues_shops', 'surplus_margin', 'meadian_profit', 'revenues_stories_new', 'surplus_margin_new', 'meadian_profit', 'revenues_stories_new1', 'surplus_margin_new1', 'meadian_profit']
It won’t be without brackets or quotes because they represent a list, and the quotes represent the strings. If you iterate through the list and print the elements, there won’t be quotes around the strings.
Below you can see the function for extracting separate words from the text. Below you can see the text and code.
import re
text = '''
def function_cal(revenues_shops, surplus_margin, meadian_profit):
median_profit= revenues_shops* surplus_margin
return median_profit
def function_cal_new (revenues_stories_new, surplus_margin_new, meadian_profit):
median_profit= revenues_stories_new* surplus_margin_new
return median_profit
def function_cal_new1 (revenues_stories_new1, surplus_margin_new1, meadian_profit):
median_profit= revenues_stories_new1* surplus_margin_new1
return median_profit
'''
def extraction_variables(text):
splited_table1, splited_table2 = dict(), dict()
lines = text.split('n')
for line in lines:
x = re.search(r"^def.*:$", line)
if x is not None:
values = x[0].split('def ')[1].split('(')
splited_table1 = values[0]
splited_table2 = values[1][:-2].split(', ')
yield splited_table1, splited_table2
splited_table1, splited_table2 = zip(*extraction_variables(text))
Now I want to put extracted words from splited_table1 and splited_table2 in the same place.
I tried with next line of code:
splited_table1+splited_table2
But after execution of the above line I have results this results:
('function_cal',
'function_cal_new ',
'function_cal_new1 ',
['revenues_shops', 'surplus_margin', 'meadian_profit'],
['revenues_stories_new', 'surplus_margin_new', 'meadian_profit'],
['revenues_stories_new1', 'surplus_margin_new1', 'meadian_profit'])
But instead of having only words separated by commas, I can see a lot of brackets. So can anybody help me how to solve this and to have only words without brackets and quotation marks?
If you want to place the words into a data structure you can use the following code (replacing the last line of code in you shared):
splited_table1, splited_table2 = zip(*extraction_variables(text))
table = []
for elem in splited_table1:
table.append(elem)
for sub_array in splited_table2:
for elem in sub_array:
table.append(elem)
print(table)
The complete code looks like this:
import re
text = '''
def function_cal(revenues_shops, surplus_margin, meadian_profit):
median_profit= revenues_shops* surplus_margin
return median_profit
def function_cal_new (revenues_stories_new, surplus_margin_new, meadian_profit):
median_profit= revenues_stories_new* surplus_margin_new
return median_profit
def function_cal_new1 (revenues_stories_new1, surplus_margin_new1, meadian_profit):
median_profit= revenues_stories_new1* surplus_margin_new1
return median_profit
'''
def extraction_variables(text):
splited_table1, splited_table2 = dict(), dict()
lines = text.split('n')
for line in lines:
x = re.search(r"^def.*:$", line)
if x is not None:
values = x[0].split('def ')[1].split('(')
splited_table1 = values[0]
splited_table2 = values[1][:-2].split(', ')
yield splited_table1, splited_table2
splited_table1, splited_table2 = zip(*extraction_variables(text))
table = []
for elem in splited_table1:
table.append(elem)
for sub_array in splited_table2:
for elem in sub_array:
table.append(elem)
print(table)
And the output looks like this:
['function_cal', 'function_cal_new ', 'function_cal_new1 ', 'revenues_shops', 'surplus_margin', 'meadian_profit', 'revenues_stories_new', 'surplus_margin_new', 'meadian_profit', 'revenues_stories_new1', 'surplus_margin_new1', 'meadian_profit']
It won’t be without brackets or quotes because they represent a list, and the quotes represent the strings. If you iterate through the list and print the elements, there won’t be quotes around the strings.