我試圖僅從函式的主體中提取單詞。下面你可以看到我的文字。
# Example of estimation
## Example of estimation
### Example of estimation
"Some calculation"
""" Note :
The data here is artificial.
Idea is to show how code will look like after estimation.
More information www.google.com
"""
@iterate_jit(nopython=True)
def fun_min_ssc(min_wage, tax_rate,calc_min_profit):
calc_min_profit= min_wage * tax_rate min_wage - (min_wage*2)
return calc_min_profit
不需要以 : #,##,###,”,""", @ 開頭的文本。
現在我只想從函式體中提取引數,例如:
- 函式名稱:fun_min_ssc和
- 函式引數:min_wage, tax_rate,calc_min_profit
我試圖用下面的函式解決這個問題:
f= open("text.txt","w ")
f.write('''# Example of estimation
## Example of estimation
### Example of estimation
"Some calculation"
""" Note :
The data here is artificial.
Idea is to show how code will look like after estimation.
More information www.google.com
"""
@iterate_jit(nopython=True)
def cal_min_ssc(min_wage, tax_rate,min_profit):
min_profit = min_wage * tax_rate min_wage - (min_wage*2)
return min_profit
''')
for line in f.readlines():
print(line, end='')
f.close()
os.getcwd()
os.listdir()
os.chdir('C:/') <---Import your path
file_reader = open('C:/text.txt') <----Import your path
os.getcwd()
# Open the file in read mode
text = open("text.txt", "r")
# Creating dictonary and count freqency
d = dict()
# Loop through each line of the file
for line in text:
# Remove the leading spaces and newline character
line = line.strip()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Split the line into words
words = line.split(" ")
words = line.split(",")
words = line.split("*")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] 1
else:
# Add the word to dictionary with count 1
d[word] = 1
# Print the contents of dictionary
for key in list(d.keys()):
print(key, ":", d[key])
那么任何人都可以幫助我如何解決這個問題或提出一些可以解決這個問題的其他方法嗎?
uj5u.com熱心網友回復:
這可能會讓你走上正軌。我已使用正則運算式陳述句作為特定的搜索條件來查找以 . 開頭def和結尾的行:。
x = re.search(r"^def.*:$", line)
一旦我有問題的線,我使用def 和函式的左括號分割線(。這使我可以輕松地獲取函式名稱。
values = x[0].split('def ')[1].split('(')
function_name = values[0]
然后我必須抓住另一部分,但洗掉最后兩個字符,即。 ):
arguments = values[1][:-2].split(', ')
由于引數用逗號分隔,因此我可以將其用作拆分分隔符。但是,我必須警告您,確保它們始終以相同的方式分隔......即逗號后有或沒有空格。
我已經列印了所需的輸出,但是,您可以將這些專案添加到串列或您想要的任何結構中:
這是我的示例代碼(沒有所有檔案輸入內容):
import re
text = '''# Example of estimation
## Example of estimation
### Example of estimation
"Some calculation"
""" Note :
The data here is artificial.
Idea is to show how code will look like after estimation.
More information www.google.com
"""
@iterate_jit(nopython=True)
def cal_min_ssc(min_wage, tax_rate, min_profit):
min_profit = min_wage * tax_rate min_wage - (min_wage*2)
return min_profit
'''
lines = text.split('\n')
for line in lines:
x = re.search(r"^def.*:$", line)
if x != None:
values = x[0].split('def ')[1].split('(')
function_name = values[0]
arguments = values[1][:-2].split(', ')
print(f"Function Name: {function_name}")
print(f"Arguments: {arguments}")
輸出:
Function Name: cal_min_ssc
Arguments: ['min_wage', 'tax_rate', 'min_profit']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/512318.html
標籤:Python细绳
下一篇:在python中反轉一個句子
