我想用熊猫字典中的不同单词替换字符串中的一个单词



请帮我解决这个问题。。我想用熊猫创建的词典中的不同单词替换字符串中的一个单词

例如

txt = "<I love *NAME*, it is *COLOR* in color, Its price is *PRICE*>"
dict = [{'NAME':'APPLE','COLOR' : 'RED', 'PRICE' : '100'},{'NAME':'MANGO','COLOR' : 'YELLOW', 'PRICE' : '300'}
output = "<I love APPLE, it is RED in color, Its price is 100><I love MANGO, it is YELLOW in color, Its price is 300>

使用的代码:

from pandas import DataFrame
import pandas as pd
txt = "<I love *NAME*, it is *COLOR* in color, Its price is *PRICE*>"
filename= r'Input.xlsx'
df = pd.read_excel(filename)
d = df.to_dict(orient='records')
for row in d:
txt=txt.replace("*NAME*",row['NAME']).replace("*COLOR*",row['COLOR']).replace("*PRICE*",str(row['PRICE']))
print(txt)

我在您的示例中使用了regex。

import re
txt = "<I love *NAME*, it is *COLOR* in color, Its price is *PRICE*>"
dicts = [{'NAME':'APPLE','COLOR' : 'RED', 'PRICE' : '100'},{'NAME':'MANGO','COLOR' : 'YELLOW', 'PRICE' : '300'}]
# output = "<I love APPLE, it is RED in color, Its price is 100><I love MANGO, it is YELLOW in color, Its price is 300>
pattern = r"*[A-Z]+*?"
matches = re.findall(pattern, txt)
output_list = []
for i in range(len(dicts)):
tempTxt = txt
for word in matches:
tempTxt = tempTxt.replace(word, dicts[i][word[1:-1]])
output_list.append(tempTxt)
output = "".join(output_list)

您也可以使用re.sub,而不是组合re.findallstr.replace。这将找到您的模式,并将匹配项替换为dict中的替换项。注意使用捕获组((...)(可以简化操作:

import re
txt = "<I love *NAME TYPE*, it is *COLOR* in color, Its price is *PRICE*>"
dicts = [{'NAME TYPE':'APPLE','COLOR' : 'RED', 'PRICE' : '100'},{'NAME TYPE':'MANGO','COLOR' : 'YELLOW', 'PRICE' : '300'}]
pattern = r"*([^*]+)*"
for d in dicts:
print(re.sub(pattern, lambda match: d[match[1]], txt))

请注意,我们使用了一个接受Match对象的函数,而不是使用显式replacment字符串。此函数只是返回dict的值,其中*之间的字符串是关键字。

Regex Demo

最新更新