python读取excel,转换成json格式,for国际化前端菜单

# -*- coding: utf-8 -*-

import pandas as pd
import json


# 读取Excel文件中的数据
excel_file = r'D:\解析excel\zy.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_pu[parent_key] = {}
        childKey = key.split(".")[1]
        special_data_pu[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', encoding='utf-8') as special_file:
    json.dump(special_data_cn, special_file, ensure_ascii=False, indent=4)# 将特殊处理的JSON对象写入txt文件
with open('special_data_en.json', 'w', encoding='utf-8') as special_file:
    json.dump(special_data_en, special_file, ensure_ascii=False, indent=4)
with open('special_data_pu.json', 'w', encoding='utf-8') 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)

结果如下:

切记,一定要安装:pip install openpyxl

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值