python - 通过 Python 将 JSON 转换为 Excel
问题描述
我有一个需要转换为 Excel 的 JSON。我正在使用带有 xlsxwriter 库的 Python 3.8。下面是示例 JSON。
{
"companyId": "123456",
"companyName": "Test",
"companyStatus": "ACTIVE",
"document": {
"employee": {
"employeeId": "EM1567",
"employeeLastName": "Test Last",
"employeeFirstName": "Test Fist"
},
"expenseEntry": [
{
"allocation": [
{
"allocationId": "03B249B3598",
"journal": [
{
"journalAccountCode": "888",
"journalPayee": "EMPL",
"journalPayer": "COMP",
"taxGuid": [
"51645A638114E"
]
},
{
"journalAccountCode": "999",
"journalPayee": "EMPL",
"journalPayer": "EMPL",
"taxGuid": [
"8114E51645A63"
]
},
],
"tax": [
{
"taxCode": "TAX123",
"taxSource": "SYST"
},
{
"taxCode": "TAX456",
"taxSource": "SYST"
}
]
}
],
"approvedAmount": 200.0,
"entryDate": "2020-12-10",
"entryId": "ENTRY9988"
}
],
"report": {
"currencyCode": "USD",
"reportCreationDate": "2020-12-10",
"reportId": "ACA849BBB",
"reportName": "Test Report",
"totalApprovedAmount": 200.0
}
},
"id": "c71b7d756f549"
}
我当前的代码: https ://repl.it/@tonyiscoming/jsontoexcel
我试过熊猫
import pandas as pd
df = pd.json_normalize(data, max_level=5)
df.to_excel('test.xlsx', index=False)
我试过 json_excel_converter
from json_excel_converter import Converter
from json_excel_converter.xlsx import Writer
conv = Converter()
conv.convert(data, Writer(file='test.xlsx'))
在这种情况下有人可以帮助我吗?太感谢了。
解决方案
在 Excel 网格中拥有空单元格并不是真正的“正确”,这就是 json_excel_converter 行为如此的原因。所以,如果你想做到这一点,恐怕你必须自己开发它。
推荐阅读
- python - 在 Jupyter Notebook(Python)的两个轴上是否有图表可以绘制非数字数据(分类数据)?
- rust - 为什么闭包返回类型定义必须包含'_生命周期?
- python - 如何从网站获取文本段落:错误 403 Forbidden
- c# - 轮播图片未加载
- flutter - 渲染 Text() 时在 Widget 中 Flutter If 语句
- arrays - Laravel 从对象中的对象访问值
- scala - 我可以用什么来更好地编写这个 scala 代码?
- android - 如何使用导航组件获取上一个目的地?
- sed - 命令行参数与 .sed 文件输入的差异
- javascript - 使用 JavaScript 在 SortableJS 可排序列表上模拟拖放