我有一个字符串格式的列名列表,如下所示:
lst = ["plug", "[plug+wallet]", "(wallet-phone)"]
现在我想使用regex将df[]
和" ' "
添加到每个列名中,我这样做了,当列表中有(wallet-phone)
这种字符串时,它会给出这样的输出df[('wallet']-df['phone')]
。我怎么会这样(df['wallet']-df['phone']),
我的模式错了。请参阅下文:
import re
lst = ["plug", "[plug+wallet]", "(wallet-phone)"]
x=[]
y=[]
for l in lst:
x.append(re.sub(r"([^+\-*\/'\d]+)", r"'\1'", l))
for f in x:
y.append(re.sub(r"('[^+\-*\/'\d]+')", r'df[\1]',f))
print(x)
print(y)
给出:
x:["'plug'", "'[plug'+'wallet]'", "'(wallet'-'phone)'"]
y:["df['plug']", "df['[plug']+df['wallet]']", "df['(wallet']-df['phone)']"]
模式不对吗? 预期产出:
x:["'plug'", "['plug'+'wallet']", "('wallet'-'phone')"]
y:["df['plug']", "[df['plug']+df['wallet']]", "(df['wallet']-df['phone'])"]
我也尝试了([^+\-*\/()[]'\d]+)
这种模式,但它并没有避免() or []
查找单词并将其包含在字典参考中可能更容易:
相关问题 更多 >
编程相关推荐