简体   繁体   English

Python:如何查找和替换字符串中的所有变量名

[英]Python: How to find and replace all variable names in a string

Suppose a I got a string from front end and such as假设我从前端得到一个字符串,例如

str='(A==1) & (B==\'A\') & (C>sin(2))'

this is the simplest format, the string could be much much more complex.这是最简单的格式,字符串可能要复杂得多。

and I would like apply the condition in dataframe filtering, such as我想在数据帧过滤中应用条件,例如

data = {'A': [1, 2, 3, 4],\
        'B': ['A','B','C','D'],\
        'C':[0.1,0.2,0.3,0.4]}
df=pd.DataFrame(data)
df_test=df[eval(str)]

To make this work, I have to find variables A,B,C in the string and replace them by df.A, df.B, df.C.为了完成这项工作,我必须在字符串中找到变量A,B,C ,并用df.A, df.B, df.C.替换它们。

I've tried the following method我试过以下方法

import ast
names = [node.id for node in ast.walk(ast.parse(str)) if isinstance(node, ast.Name)]
print(names)

but it returns ['C', 'A', 'B', 'sin'] in which 'sin' is not required.但它返回['C', 'A', 'B', 'sin']其中 'sin' 不是必需的。

I also tried pyparse but still can not figure out how to define the pattern of variable name.我也尝试过pyparse但仍然无法弄清楚如何定义变量名的模式。

It will be much appreciated if you can help to give me some advice on how to find and replace the variable name in string?如果您能帮助我就如何查找和替换字符串中的变量名提供一些建议,我们将不胜感激?

You can use an ast.NodeTransformer to make the replacements:您可以使用ast.NodeTransformer进行替换:

import ast
s = '(A==1) & (B==\'A\') & (C>sin(2))'
data = {'A': [1, 2, 3, 4], 'B': ['A', 'B', 'C', 'D'], 'C': [0.1, 0.2, 0.3, 0.4]}
class toDf(ast.NodeTransformer):
    def visit_Name(self, node):
       if node.id in data: #check if variable name exists in the data
           node = ast.Attribute(value=ast.Name(id='df'), attr=node.id)
       return node

new_s = ast.unparse(toDf().visit(ast.parse(s)))
print(new_s)

Output:输出:

(df.A == 1) & (df.B == 'A') & (df.C > sin(2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM