python数据提取代码_如何使用python从代码中提取数据

下面是一个在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)

结果是:

^{pr2}$

我觉得从这里走比较容易

(我不需要以哪种形式提供数据,例如括号是否重要?)在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值