如何使用python从代码中提取数据
我有一些函数,比如用easy语言编写的.txt文件,我需要用python从这些函数中提取数据。作为一个例子,考虑以下部分: 代码段-如何使用python从代码中提取数据,python,text-extraction,Python,Text Extraction,我有一些函数,比如用easy语言编写的.txt文件,我需要用python从这些函数中提取数据。作为一个例子,考虑以下部分: 代码段- If MarketPosition = 0 and (EntriesToday(Date) < 1 or EndofSess) and EntCondL then begin Buy("EnStop-L") NShares shares next bar at EntPrL stop; end; 如果MarketPosition=0且(Entri
If MarketPosition = 0 and (EntriesToday(Date) < 1 or EndofSess) and
EntCondL
then begin
Buy("EnStop-L") NShares shares next bar at EntPrL stop;
end;
如果MarketPosition=0且(EntriesToday(Date)<1或Endofess)且
第二次
然后开始
在EntPrL站购买(“EnStop-L”)NShares股票;
结束;
在这里,我需要提取零件
- MarketPosition=0
- EntriesToday(日期)<1
- endofess
- EntCondL
并识别
=
,这里有一个示例,用于查找和拆分if
和then
结果是单个元素的列表:变量、括号和比较运算符
code = """
If MarketPosition = 0 and (EntriesToday(Date) < 1 or EndofSess) and
EntCondL
then begin
Buy("EnStop-L") NShares shares next bar at EntPrL stop;
end;
"""
import re
words = re.split("\s+|(\(|\)|<|>|=|;)", code)
is_if = False
results = []
current = None
for token in words:
if not token:
continue
elif token.lower() == "if":
is_if = True
current = []
elif token.lower() == "then":
is_if = False
results.append(current)
elif is_if:
if token.isdecimal(): # Detect numbers
try:
current.append(int(token))
except ValueError:
current.append(float(token))
else: # otherwise just take the string
current.append(token)
print(results)
code=”“”
如果MarketPosition=0和(EntriesToday(Date)<1或Endofess)和
第二次
然后开始
在EntPrL站购买(“EnStop-L”)NShares股票;
结束;
"""
进口稀土
words=re.split(“\s+|(\(|\)|=|”)”,代码)
is_if=False
结果=[]
电流=无
以文字表示的代币:
如果不是令牌:
持续
elif token.lower()=“如果”:
如果=真的话
当前=[]
elif token.lower()=“然后”:
is_if=False
results.append(当前)
如果:
if token.isdecimal():#检测数字
尝试:
当前.append(int(令牌))
除值错误外:
当前.附加(浮动(令牌))
否则就拿绳子吧
当前.append(令牌)
打印(结果)
结果是:
['MarketPosition', '=', 0, 'and', '(', 'EntriesToday', '(', 'Date', ')', '<', 1, 'or', 'EndofSess', ')', 'and', 'EntCondL']
['MarketPosition'、'='、0'和'、'('、'EntriesToday'、'('、'Date'、')、'),'我想您正在寻找一些运算符的前缀和后缀
我建议您找到这些运算符列表,并使用它的位置来获取前缀和后缀您可能需要一个语言解析器,或者它总是在If…then
之间,或者这只是一个示例?@Venify它总是在If…then和end之间;正如您所说,括号实际上并不重要……我能够通过您的c完成这项工作再次感谢你