标签:param lse 过程 span class nbsp value load 正则
一、提取 ${} 之间的内容
1、正则表达式
2、用 Python的正则 提取
二、替换 ${} 之间的内容
1、替换
2、封装成专门的替换函数
import re def my_split(resource_data: str, split_content: dict): """ :param resource_data: 被替换的原数据 :param split_content: 需要替换的内容 """ matches = re.findall(r"(?<=\$\{).*?(?=\})", resource_data) # 提取需要替换的目标 new_data = None # 替换后的数据 for i in matches: # 替换过程 new_value = split_content[i] if i == matches[0]: new_data = resource_data.replace("${" + i + "}", str(new_value)) else: new_data = new_data.replace("${" + i + "}", str(new_value)) return new_data # 开始调试 old_data = ‘{"access-token": "${token}", "data": ${user_id}, "phone_code": ${code}}‘ spilt_data = {"token": "123asf6549qewA+-*asd", "user_id": [1, 2, 3, 4], "code": 1356} new_data0 = my_split(old_data, spilt_data) print(new_data0) # {"access-token": "123asf6549qewA+-*asd", "data": [1, 2, 3, 4], "phone_code": 1356} data_dict = eval(new_data0) print(type(data_dict["access-token"])) # <class ‘str‘> print(type(data_dict["data"])) # <class ‘list‘> print(type(data_dict["phone_code"])) # <class ‘int‘>
标签:param lse 过程 span class nbsp value load 正则
原文地址:https://www.cnblogs.com/mua9102/p/13369278.html