json文件
import json
import random
import collections
filename = '在线请求0426.txt'
# 将数据加载到一个列表中
#首先得到所有的商家,把商家设置一个状态,存到一个map里
poiid_status_maps={}
poiid_shelf_maps= collections.defaultdict(list)
shelf_poiid_maps= collections.defaultdict(list)
with open(filename) as f:
origin_data1 = json.load(f)
reqItemList = origin_data1["reqItemList"]
for i in range(len(reqItemList)):
subItem_list = reqItemList[i]["subItem"] # 货架下的商家list
for j in range(len(subItem_list)):
poi_id = subItem_list[j]["itemId"]
if poi_id not in poiid_status_maps:
poi_status_index = random.randint(0,2)
poiid_status_maps[poi_id] = str(poi_status_index)
poiid_shelf_maps[poi_id].append(reqItemList[i]["itemId"])
shelf_poiid_maps[reqItemList[i]["itemId"]].append(poi_id)
print(poiid_shelf_maps)
print(shelf_poiid_maps)
with open(filename) as f:
origin_data = json.load(f)
#在请求中加入商家状态,reqItemList->subItem->attribute
reqItemList = origin_data["reqItemList"]
print(reqItemList)
for i in range(len(reqItemList)):
subItem_list = reqItemList[i]["subItem"] #货架下的商家list
for j in range(len(subItem_list)):
cur_poi_id = subItem_list[j]["itemId"]
cur_poi_id_status = poiid_status_maps[cur_poi_id]
maps1={}
maps1["poiStatus"] = cur_poi_id_status
subItem_list[j]["attribute"] =maps1
print(origin_data)
# res_path = "在线请求0426_test1.txt"
res_path = open('在线请求0426_test1.json','w',encoding='utf-8')
json.dump(origin_data, res_path)
xml文件
dense_back_alist = ["refer_spu_diff_price","refer_spu_price_sum","refer_spu_sku_count_sum",
"sg_product_click_cnt_1days", "sg_product_cart_cnt_1days",
"sg_product_click_cnt_7days", "sg_product_cart_cnt_7days"
]
dense_back_str = ''
for i in range(len(dense_back_alist)):
dense_back_str += dense_back_alist[i]
if i < len(dense_back_alist) - 1:
dense_back_str += ','
from xml.dom import minidom
import xml.etree.ElementTree as ET
from lxml.etree import Element
path1 = 'tensors_test.xml'
# <?xml version='1.0' encoding='utf-8'?>
# <conf xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn http://s3plus.vip.sankuai.com/v1/mss_bc493fbe6c094957a9f7fdec59e4d560/meituan-waimai-d-feature-platform/feature.xsd">
# <features>
# <field>
# <name>build_feature/dense</name>
# <type>float</type>
# <feature>a,b</feature>
# </field>
# <features>
ET.register_namespace('', "http://www.w3school.com.cn")
ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
xml_file=path1
tree = ET.parse(xml_file)
features=tree.find('{http://www.w3school.com.cn}features')
field=features.findall('{http://www.w3school.com.cn}field')
for ix, obj in enumerate(field):
name = obj.find('{http://www.w3school.com.cn}name').text
feature = obj.find('{http://www.w3school.com.cn}feature').text
#当<name>是这个的实话,修改<feature>的值
if name == 'build_feature/dense':
obj.find('{http://www.w3school.com.cn}feature').text = dense_back_str
path2 = 'tensors_test_a1.xml'
tree.write(path2, encoding='utf-8',xml_declaration=True)