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

2024-09-30 12:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些函数,比如用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;

在这里,我需要提取零件

  • 市场地位=0
  • 项目日期<;1
  • 内啮合
  • EntCondL

并使用python识别=<符号。 提前谢谢。


Tags: orand文件数据函数txt语言date
2条回答

我想你在寻找一些运算符的前缀和后缀
我建议您找到这些运算符列表并使用它的位置来获取前缀和后缀

下面是一个在ifthen之间查找和拆分文本的示例 结果是单个元素的列表:变量、方括号和比较运算符。在

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}$

我觉得从这里走比较容易 (我不需要以哪种形式提供数据,例如括号是否重要?)在

相关问题 更多 >

    热门问题