一段字符处理代码,推荐数据的多标签变成字典
原始的数据格式:
photo_id,tag_info_list
25916978799,"[{""tag_id"":2001,""tag_name"":""婴儿"",""prob"":0.080000006},{""tag_id"":2624,""tag_name"":""正面"",""prob"":0.089999996}]"
filename = "E:\python_code\kuaishou\kuaishou_data\photo_tag.txt"
item_dict = dict()
with open(filename, 'rb') as file_to_read:
i = 0
while True:
lines = file_to_read.readline()
i += 1
if i != 1:
lines = str(lines, encoding='utf-8')
if not lines:
break
position = lines.find(',')
p_tmp = lines[: position]
E_tmp = lines[position + 1:]
E_list = E_tmp[2: -4].replace("\"\"", "\"")
E_list = E_list.split("},")
tag_id = list()
for i in range(len(E_list) - 1):
E_list[i] += "}"
for i in range(len(E_list)):
current_dict = eval(E_list[i])
tag_id.append(int(current_dict["tag_id"]))
# print(tag_id)
item_dict[str(p_tmp)] = tag_id
pass
with open("../item_dict.json", 'w') as f:
json.dump(item_dict, f)

浙公网安备 33010602011771号