向列名添加特殊字符

Adding special character to a column names

我有一个字符串格式的列名列表,如下所示:

lst = ["plug", "plug+wallet", "wallet-phone"]

我想添加 df[]" ' ". 我正在使用正则表达式来替代它。但是当列表如下时,我使用的正则表达式工作正常:-

lst = [" 'plug'", "'plug'+'wallet'", "'wallet'-'phone'"]
x=[]
for l in lst: x.append(re.sub(r"('[^+\-*\/'\d]+')", r'df[]',l))
print(x)

结果如期

x: [" df['plug']", "df['plug']+df['wallet']", "df['wallet']-df['phone']"]

但是当列表是这样的时候:

lst = ["plug", "plug+wallet", "wallet-phone"]
x=[]
y=[]
for l in lst: x.append(re.sub(r"('[^+\-*\/'\d]+')", r'',l))
for f in x:    y.append(re.sub(r"('[^+\-*\/'\d]+')", r'df[]',f))
print(x)
print(y)

这给出:

['plug', 'plug+wallet', 'wallet-phone']
['plug', 'plug+wallet', 'wallet-phone']

我哪里错了?我是否遗漏了第一个正则表达式模式中的任何内容或未正确传递 r''

异常输出:

x: [" 'plug'", "'plug'+'wallet'", "'wallet'-'phone'"]    
y: [" df['plug']", "df['plug']+df['wallet']", "df['wallet']-df['phone']"]

这个有效:

import re
lst = ["plug", "plug+wallet", "wallet-phone"]
x = [re.sub(r"([^+\-*\/'\d]+)", r"''", l) for l in lst]
y = [re.sub(r"('[^+\-*\/'\d]+')", r"df[]", l) for l in x]
print(x)
print(y)

您的第一个正则表达式在 '' 上错误匹配,然后在替换主题中没有将其包含在 ''.

在 Python 3.8.0.

下测试