【python】Excel转json「建议收藏」
python,Excel,json,建议,收藏
2025-04-01 16:27:54 时间
大家好,又见面了,我是你们的朋友全栈君。
表格样式:无表头数据
import xlrd, json, os
data = xlrd.open_workbook("working_file.xls")
public = []
tables = [(excelName, data.sheet_by_name(excelName)) for excelName in data.sheet_names()]
# 表由多个相同的表,数据为两列组成
filePath = "picture_folder_path"
# 图片路径
for index, dataSet in tables:
data = []
count = 0
imgName = ""
imgfiles = os.listdir(os.path.join(filePath, index))
for row in range(dataSet.nrows):
item = dataSet.row_values(row) # 获取当前行
count += 1
# 图片路径,判断图片是否有png或jpg格式,如果没有着返回异常图片名和异常位置
strName = "".join(filter(str.isalnum, item[1])).lower()
if "%s.png"%strName in imgfiles:
imgName = "%s.jpg"%strName
elif "%s.jpg"%strName in imgfiles:
imgName = "%s.jpg"%strName
else:
print(strName, index, "It is Error")
# 第三层打包
data.append({
"id": "",
"order": "{:0>3d}".format(count),
"name": item[1],
"str" : strName,
"url": item[0],
"img": os.path.join("img", index, imgName)
})
# 第二层打包
public.append({
index: {
"id": "",
"data": data
}
})
# 第一层打包
pack = [{
"code": 200,
"data": public
}]
# 数据转为接送格式
rest = json.dumps(pack, indent=4)
with open("Data.json", mode="w") as f:
f.write(rest)
print("Run successfully and convert")
输出的结果如下图所示:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/134440.html原文链接:https://javaforall.cn
相关文章
- linux fsync实例,Python os.fsync()方法「建议收藏」
- Python 图_系列之基于<链接表>实现无向图最短路径搜索
- Python open函数详解「建议收藏」
- 2022春节贺岁档电影开分,水门桥不理想,四海崩了!用Python一探究竟
- 视频识别车牌号(Python)
- Python爬取热搜数据之炫酷可视化[通俗易懂]
- python:最大公约数和最小公倍数
- Python一键生成国庆渐变头像
- 使用Python检测符号及乱码字符
- 最美圣诞树!用Python画棵雪夜圣诞树送给你
- 地球科学领域Python工具合集
- Python批量爬取淘宝商品评价信息
- Python 上下文管理及 with 语句的实用技巧
- python时间和日期操作(datetime和monthrange,timedelta)
- Python之谜:四舍五入round(4.5)等于4?
- Python 链接/操作 MongoDB 数据库
- Anaconda中将python 3.7版本退回python 3.6版本
- Python进阶42-drf框架(四)
- 不要再写Python for循环了
- [Python图像处理] 十一.灰度直方图概念及OpenCV绘制直方图