首页 > 编程语言 >python正则表达式获取子字符串

python正则表达式获取子字符串

时间:2022-11-02 17:58:54浏览次数:44  
标签:item python 正则表达式 test 字符串 df reply model col

import re
import pandas as pd
false = False
true = True

df = pd.read_excel("被替换.xlsx")
test = pd.read_excel("test.xlsx")

# 获取已修改数据
test_list = []
for col in test.columns:
    col = test[col]
    for item in col:
        model = re.findall('"iotmodel": (.*?),',item)[0]
        id = re.findall('"recordId": (.*?),', item)[0]
        reply = re.findall('"smr_reply": (.*?),', item)[0]
        test_list.append({'model': model, 'id': id, 'reply': reply})


# 修改数据
def typecof(x):
    item = eval(x)
    df_model = item["iotmodel"]
    for i in test_list:
        if df_model == i['model']:
            item['recordId'] = i['id']
            item['smr_reply'] = i['reply']
    return item
for col in df.columns:
    df[col] = df[col].apply(lambda x: typecof(x))


# 导出
df.to_excel("output.xlsx", index=False)

标签:item,python,正则表达式,test,字符串,df,reply,model,col
From: https://www.cnblogs.com/tiansz/p/16851813.html

相关文章