# -*- coding: utf-8 -*-
import pandas as pd
import json
# 读取Excel文件中的数据
excel_file = r'D:\解析excel\中英.xlsx'
df = pd.read_excel(excel_file)
# 生成中文JSON和英文JSON
cn_data = {}
en_data = {}
pu_data = {}
special_data_cn = {}
special_data_en = {}
special_data_pu = {}
for index, row in df.iterrows():
key = str(row.iloc[0]) # 强制将key转换为字符串类型
value_cn = row.iloc[1]
value_en = row.iloc[2]
value_pu = row.iloc[3]
if "." in key: # 如果键名中包含 "."
parent_key, sub_key = key.split(".", 1) # 分割键名为父键和子键
if parent_key not in special_data_cn:
special_data_cn[parent_key] = {}
childKey = key.split(".")[1]
special_data_cn[parent_key][childKey] = value_cn
if "." in key: # 如果键名中包含 "."
parent_key, sub_key = key.split(".", 1) # 分割键名为父键和子键
if parent_key not in special_data_en:
special_data_en[parent_key] = {}
childKey = key.split(".")[1]
special_data_en[parent_key][childKey] = value_en
if "." in key: # 如果键名中包含 "."
parent_key, sub_key = key.split(".", 1) # 分割键名为父键和子键
if parent_key not in special_data_pu:
special_data_en[parent_key] = {}
childKey = key.split(".")[1]
special_data_en[parent_key][childKey] = value_pu
else:
cn_data[key] = value_cn
en_data[key] = value_en
pu_data[key] = value_pu
# 将特殊处理的JSON对象写入txt文件
with open('special_data_cn.json', 'w') as special_file:
json.dump(special_data_cn, special_file, ensure_ascii=False, indent=4)# 将特殊处理的JSON对象写入txt文件
with open('special_data_en.json', 'w') as special_file:
json.dump(special_data_en, special_file, ensure_ascii=False, indent=4)
with open('special_data_pu.json', 'w') as special_file:
json.dump(special_data_pu, special_file, ensure_ascii=False, indent=4)
# 将中文JSON对象写入txt文件
with open('cn_data.json', 'w', encoding='utf-8') as cn_file:
json.dump(cn_data, cn_file, ensure_ascii=False, indent=4)
# 将英文JSON对象写入txt文件
with open('en_data.json', 'w', encoding='utf-8') as en_file:
json.dump(en_data, en_file, ensure_ascii=False, indent=4)
# 将葡萄牙JSON对象写入txt文件
with open('pu_data.json', 'w', encoding='utf-8') as pu_file:
json.dump(pu_data, pu_file, ensure_ascii=False, indent=4)
结果如下: