excel填数据转json格式
定制化比较严重,按需更改
excel文件如下

代码
# -*- coding: utf-8 -*-
import oss2
import shutil
import sys
import xlwt
import xlrd
import json
from datetime import datetime, timedeltafile1 = "C:\\Users\\cxy\\Desktop\\generate.xls"
#打开表1
wb1 = xlrd.open_workbook(filename=file1)# 表1的sheet
sheet = wb1.sheet_by_index(1)
# 表1的sheet的总行数
rowNum = sheet.nrows
# 表1的sheet的总列数
colNum = sheet.ncolsjson_data = {}def getCellValue(row,col): # if row == 8:# print(1)value = sheet.cell_value(row,col)if str(value).endswith(".0"):return str(value).split(".0")[0]# if type(value) == float:# value = '%.2f' % sheet.cell_value(8,8)return value
def getRowValue(row):return sheet.row_values(row)
def getColValue(col,start_rowx=0, end_rowx=None):return sheet.col_values(col, start_rowx, end_rowx)
def isKeyRow(row):return any(s == '$$key$$' for s in sheet.row_values(row))
import redef name_convert_to_camel(name: str) -> str:"""下划线转驼峰(小驼峰)"""return re.sub(r'(_[a-z])', lambda x: x.group(1)[1].upper(), name)def arr_str_to_arr(value: str) -> str:"""数组字符串变成数组类型"""value = str(value)if re.match('^\[', value) and re.search('\]$', value):arr = value.strip("[]").split(",")temp = []for v in arr:temp.append({"name":add_oss_host(v)})return tempreturn add_oss_host(value)def add_oss_host(value: str) -> str:content = str(value)if content.endswith(".png") or content.endswith(".jpeg") or content.endswith(".mp4") or content.endswith(".jpg"):return "/s" + contentelse:return contentdef resolveData(cur_level,start_row,end_row,data):col_values = sheet.col_values(cur_level, start_row, end_row)cur_level_key_row = [i+start_row for i in range(0,len(col_values)) if col_values[i] == '$$key$$']# cur_level_key_row.append(rowNum)for index, row_index in enumerate(cur_level_key_row):key_row = getRowValue(row_index)key = key_row[cur_level+1]if str(key).endswith(".0"):key = str(key).split(".0")[0]type = key_row[cur_level+2]if isKeyRow(row_index+1):# 第一行就遇到keyif type == 'array':print("第"+(row_index+1)+"行格式不对")breakelse:temp = {}data[key] = tempif row_index+1<rowNum and isKeyRow(row_index+1):resolveData(cur_level + 1, row_index + 1, rowNum if (index + 1) >= len(cur_level_key_row) else cur_level_key_row[index + 1], temp)continuecolumn_row = getRowValue(row_index+1)column_name_arr = []for col_index in range(cur_level,colNum):column_name = column_row[col_index]if column_name == '':breakcolumn_name_arr.append(name_convert_to_camel(column_name))if type == 'object':temp = {}data[key] = tempfor i in range(0,len(column_name_arr)):temp[column_name_arr[i]] = arr_str_to_arr(getCellValue(row_index+2, i + cur_level) )if row_index+3<rowNum and isKeyRow(row_index+3):resolveData(cur_level + 1, row_index + 3, rowNum if (index + 1) >= len(cur_level_key_row) else cur_level_key_row[index + 1], temp)else:# [0, 18, 28]tempArr = []data[key] = tempArrarr_data_start = cur_level_key_row[index]+2arr_data_end = rowNum # 默认,下面会改next_key_index_temp = rowNumif (index + 1) < len(cur_level_key_row):next_key_index_temp = cur_level_key_row[index + 1]if cur_level == 0:arr_data_end = next_key_index_tempelse:for i_temp in range(arr_data_start, next_key_index_temp):if i_temp == rowNum-1:breakif (getCellValue(i_temp, cur_level-1) == '' and getCellValue(i_temp+1, cur_level-1) != '') or (isKeyRow(i_temp+1) and getCellValue(i_temp+1, cur_level) == '$$key$$'):arr_data_end = i_temp+1breakfor chi_row_index in range(arr_data_start, arr_data_end):colValue = getCellValue(chi_row_index, cur_level)if colValue == '':continuetemp = {}tempArr.append(temp)for i in range(0,len(column_name_arr)):temp[column_name_arr[i]] = arr_str_to_arr(getCellValue(chi_row_index, i + cur_level) )if chi_row_index + 1 < rowNum and isKeyRow(chi_row_index + 1):parentColValues = getColValue(cur_level, chi_row_index + 1, rowNum)not_empty_index = next((j for j, v in enumerate(parentColValues) if v), len(parentColValues))resolveData(cur_level + 1, chi_row_index + 1, chi_row_index + not_empty_index + 1, temp)cur_level = 0
start_row = 0
end_row = rowNum
json_data = {}resolveData(cur_level,start_row,end_row,json_data)print(json.dumps(json_data, ensure_ascii=False))target = "C:\\Users\\cxy\\Desktop\\generate_target.json"
with open(target, "w", encoding='utf-8') as f:json.dump(json_data, f, indent=4, ensure_ascii=False)
结果
{
"userInfos": {
"userPic": "https://oss.shop.sxmu.com/test/dzpt/头像20.jpeg",
"nickName": "墨雨无痕",
"level": "42",
"userId": "66606024",
"gaming_career": [
{
"gamePic": "https://oss.shop.sxmu.com/test/dzpt/mszb.jpeg",
"gameName": "魔兽争霸3",
"level": "42",
"score": "96800",
"gaming_career": [
{
"modePic": "https://oss.shop.sxmu.com/test/dzpt/ch.png",
"modeName": "澄海3C",
"score": "56800",
"gameTimes": "1000"
},
{
"modePic": "https://oss.shop.sxmu.com/test/dzpt/DOTA6.83.png",
"modeName": "DOTA 6.83",
"score": "40000",
"gameTimes": "650"
}
]
},
{
"gamePic": "https://oss.shop.sxmu.com/test/dzpt/cs.jpg",
"gameName": "CS",
"level": "38",
"score": "76500"
}
]
},
"player_list": {
"star_list": [
{
"userPic": "https://oss.shop.sxmu.com/test/dzpt/头像11.jpeg",
"nickName": "阿海",
"userDesc": "喜欢跳不上的b小,架不住的a1,最爱的沙鹰,放不开也抓不住。",
"online": "0"
},
{
"userPic": "https://oss.shop.sxmu.com/test/dzpt/头像12.jpeg",
"nickName": "醉念",
"userDesc": "只不过游戏而已,玩的再牛逼又如何",
"online": "0"
}
],
"fans_list": [
{
"userPic": "https://oss.shop.sxmu.com/test/dzpt/头像16.jpeg",
"nickName": "亡梦",
"userDesc": "遗憾与残局皆,与神明画过押。",
"online": "0"
}
]
}
}
相关文章:
excel填数据转json格式
定制化比较严重,按需更改 excel文件如下 代码 # -*- coding: utf-8 -*- import oss2 import shutil import sys import xlwt import xlrd import json from datetime import datetime, timedeltafile1 "C:\\Users\\cxy\\Desktop\\generate.xls" #打开表…...
解决echarts和v-show一起使用canvas宽高改变
本来是想没有数据显示暂无数据的,结果显示成了这样 1.把V-show改成v-if <template><divclass"chart1"ref"chart1"v-if"!nodata"style"width: 100%; height: 100%"></div><el-empty description&quo…...
typescript 中的数据类型有哪些?
目录 1. 介绍2.总结 话不多说 直接开冲 干干干! 1. 介绍 typescript 的数据类型主要有如下: boolean(布尔类型)number(数字类型)string(字符串类型)array(数组类型&…...
RabbitMQ-消息中间件学习记录(what-how-why)
什么是消息中间件 简单的来说就是消息队列中间件,生产者发送消息到中间件,消息中间件用于保存消息并发送消息到消费者。 消息中间件RabbitMQ的基本组件 1)producer -生产者 2)customer -消费者 3)broker (经纪人)- MQ…...
前端HTML进阶
day02:列表、表格、表单 目标:掌握嵌套关系标签的写法,使用列表标签布局网页 01-列表 作用:布局内容排列整齐的区域。 列表分类:无序列表、有序列表、定义列表。 无序列表 作用:布局排列整齐的不需要规…...
Python“牵手”lazada商品详情页数据采集方法,lazadaAPI接口申请指南
lazada详情接口 API 是开放平台提供的一种 API 接口,它可以帮助开发者获取商品的详细信息,包括商品的标题、描述、图片等信息。在电商平台的开发中,详情接口API是非常常用的 API,因此本文将详细介绍详情接口 API 的使用。 一、la…...
买机票系统---(java实现)
/* * 案例 * 卖机票 * 需求:机票价格按照淡季和旺季,头等舱和经济舱收费,输入机票原价,月份和头等舱或经济舱 * 旺季(5-10月):头等舱9折,经济舱8.5折 * 淡季(11-来年4月&…...
“new出对象“原理的深层解密
🎈个人主页:🎈 :✨✨✨初阶牛✨✨✨ 🐻推荐专栏1: 🍔🍟🌯C语言初阶 🐻推荐专栏2: 🍔🍟🌯C语言进阶 🔑个人信条: 🌵知行合一 …...
Java基础篇--SecureRandom(安全随机)类
java.security.SecureRandom类是Java中用于生成安全的随机数的一个类。与普通的Random类不同,它提供了一种可信赖的随机数生成器,用于生成具有高度随机性的随机数。 SecureRandom类的实例使用了更加安全的随机数生成算法,这些算法通常经过密…...
论文复现--关于多视角动作捕捉工具箱 --XRMoCap的研究
分类:动作捕捉 github地址:https://github.com/openxrlab/xrmocap 所需环境: Ubuntu18.04,conda22.9.0,CUDA11.4 目录 环境配置 环境配置 conda create -n XRmocap python3.7 -y conda activate XRmocap# install ffm…...
Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换
最近考虑项目在不同环境下配置的切换,使用profile注解搭配PropertyPlaceholderConfigurer实现对配置文件的切换,简单写了个demo记录下实现。 基本知识介绍 Profile Profile通过对bean进行修饰,来限定spring在bean管理时的初始化情况&#…...
ansible入门
ansible入门 一.ansible 背景介绍 Ansible 是一个广受欢迎的 IT 自动化系统。可以用来处理配置管理、应用自动化部署、云资源配给、网络 自动化和多借点部署等任务。其也可以使得复杂的变更如带负载均衡的零停机滚动更新更加容易。Ansible.com 1.1 自动化运维概念 1.1.1 运维…...
用Node.js吭哧吭哧撸一个运动主页
简单唠唠 某乎问题:人这一生,应该养成哪些好习惯? 问题链接:https://www.zhihu.com/question/460674063 如果我来回答肯定会有定期运动的字眼。 平日里也有煅练的习惯,时间久了后一直想把运动数据公开,…...
【C++】STL---vector
STL---vector 一、vector 的介绍二、vector 的模拟实现1. 容量相关的接口(1)size(2)capacity(3)reserve(4)resize(5)empty 2. [] 重载3. 迭代器4. 修改数据相…...
机器学习:基本介绍
机器学习介绍 Hnad-crafted rules Hand-crafted rules,叫做人设定的规则。那假设今天要设计一个机器人,可以帮忙打开或关掉音乐,那做法可能是这样: 设立一条规则,就是写一段程序。如果输入的句子里面看到**“turn of…...
基于长短期神经网络LSTM的碳排量预测,基于LSTM的碳排放量预测
目录 背影 摘要 LSTM的基本定义 LSTM实现的步骤 基于长短期神经网络LSTM的碳排放量预测 完整代码: 基于长短期神经网络LSTM的碳排放量预测,基于LSTM的碳排放量预测资源-CSDN文库 https://download.csdn.net/download/abc991835105/88184632 效果图 结果分析 展望 参考论文 背…...
日常BUG——SpringBoot关于父子工程依赖问题
😜作 者:是江迪呀✒️本文关键词:日常BUG、BUG、问题分析☀️每日 一言 :存在错误说明你在进步! 一、问题描述 在父子工程A和B中。A依赖于B,但是A中却无法引入B中的依赖,具体出现的…...
Zabbix监控tomcat
文章目录 一、安装部署TomcatTomcat二、安装Tomcat1.安装zabbix-agent收集监控数据(192.168.40.104)2.安装部署Zabbix-server(192.168.40.105)3.配置数据库 三、Zabbix监控Tomcat页面设置 实验环境 主机用途Centos7:192.168.40.105zabbix-server,zabbix-java-gatew…...
CentOS-6.3安装MySQL集群
安装要求 安装环境:CentOS-6.3 安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz 下载地址:http://mysql.mirror.kangaroot.net/Downloads/ 软件安装位置:/usr/local/mysql 数据存放位…...
用PyTorch复现FactorVAE:一个能同时预测收益和风险的量化模型实战教程
用PyTorch实战FactorVAE:构建收益与风险双预测的量化模型 在量化投资领域,传统线性因子模型正逐渐被非线性机器学习方法所取代。然而金融数据特有的低信噪比特性,使得直接从市场数据中提取有效因子成为一项艰巨挑战。本文将深入探讨如何利用P…...
BiliBiliCCSubtitle终极指南:5个实战技巧高效下载B站字幕
BiliBiliCCSubtitle终极指南:5个实战技巧高效下载B站字幕 【免费下载链接】BiliBiliCCSubtitle 一个用于下载B站(哔哩哔哩)CC字幕及转换的工具; 项目地址: https://gitcode.com/gh_mirrors/bi/BiliBiliCCSubtitle 还在为无法保存B站视频字幕而烦恼࿱…...
Claude Code用户告别封号与Token焦虑,无缝切换至Taotoken平台
🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 Claude Code用户告别封号与Token焦虑,无缝切换至Taotoken平台 对于依赖Claude Code进行编程辅助的开发者而言ÿ…...
Godot 2D随机地图三大静默故障:黑屏、穿墙、寻路失败的根源与修复
1. 为什么刚上手Godot做2D随机地图就总卡在“生成出来是黑的”“角色穿墙”“房间连不通”这三件事上?如果你是刚从Unity或GameMaker转来Godot,或者第一次用GDScript写程序逻辑的新手,大概率已经在2D随机地图生成这个环节反复摔过跟头——不是…...
C++的单例模式及其作用
什么是单例模式?无论是在面向对象编程还是软件架构中,单例模式都扮演着至关重要的角色。它不仅能够确保一个类只有一个实例存在,还能够提供全局访问点,使得我们可以方便地在程序的任何地方使用该实例。但有几个设计模式并非解决抽…...
Unity Cinemachine相机系统深度使用:除了自动跟随,它的边界限制(Confiner)功能才是宝藏
Unity Cinemachine Confiner:解锁专业级镜头边界控制的实战指南在游戏开发中,镜头控制往往是被低估的艺术。许多开发者对Cinemachine的印象停留在"智能跟随相机"层面,却不知道它的Confiner功能能够彻底改变游戏镜头的专业度。想象一…...
Windows多显示器DPI缩放终极解决方案:告别模糊显示,享受清晰视觉体验
Windows多显示器DPI缩放终极解决方案:告别模糊显示,享受清晰视觉体验 【免费下载链接】SetDPI 项目地址: https://gitcode.com/gh_mirrors/se/SetDPI 你是不是曾经遇到过这样的困扰?连接多个显示器时,文字和图标大小不一&…...
ComfyUI扩展生态系统的智能管家:ComfyUI-Manager全面解析
ComfyUI扩展生态系统的智能管家:ComfyUI-Manager全面解析 【免费下载链接】ComfyUI-Manager ComfyUI-Manager is an extension designed to enhance the usability of ComfyUI. It offers management functions to install, remove, disable, and enable various cu…...
Wand-Enhancer:3步解锁WeMod专业版功能的完整用户指南
Wand-Enhancer:3步解锁WeMod专业版功能的完整用户指南 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/gh_mirrors/we/Wand-Enhancer 你是否厌倦了WeMod免费版的种种限制&a…...
量子机器学习在时间序列预测中的表现:一项基准研究的深度解析
1. 项目概述与核心问题 时间序列预测,这个听起来有点学术的词,其实离我们并不远。从明天股市的涨跌,到下周的天气变化,再到工厂里一台机器的故障预警,背后都离不开对历史数据的分析和未来趋势的推断。在经典计算领域&a…...
