python - Excel 到 JSON 的转换
问题描述
此excel 文件已转换为 json,但使用 python 中的 pandas 库,它会明智地执行转换列并显示如下。我希望它以以下格式显示:
{"peopleInfo": [
{
"Name": "John",
"UserId": "39048",
"FavoriteWords": [
{
"word": "Sincerity",
"wordInfo": [
{
"meaning": "the quality or state of being sincere",
"language": "English"
},
{
"meaning": "honesty of mind",
"language": "English"
}, and so on....
]
},
{
"word": "Sincerity",
"wordInfo": [
{
"meaning": "the quality or state of being sincere",
"language": "English"
},
{
"meaning": "honesty of mind",
"language": "English"
}
]
}, and so on....
]
},
{
"Name": "Doe",
"UserId": "23749",
"FavoriteWords": [
{
"word": "Fun",
"wordInfo": [
{
"meaning": "what provides amusement or enjoyment",
"language": "English"
},
{
"meaning": "a mood for find or making amusement",
"language": "English"
}
]
},
{
"word": "Sharing",
"wordInfo": [
{
"meaning": "to divide something between two or more people",
"language": "English"
},
{
"meaning": "joint use of a resource or space",
"language": "English"
}
]
}
]
}, and so on....
] }
请不要考虑问题的上下文,我知道它很蹩脚,但我正在做一些不同的事情。
解决方案
.csv 的结构可以在 python 中显示为字典。作为文件,您可以为此使用嵌套的 .json或 .xml。
.csv 中的嵌套 .json 可以csv.DictReader
在csv 中生成嵌套 JSON 吗?
.json
是一种序列化格式,.csv 文件的结构是嵌套的 .json、字典或 .xml 的结构。要使用 pandas 生成 .xml,请使用模块进行.xml
处理,例如xml.etree.ElementTree
https://stackabuse.com/reading-and-writing-xml-files-in-python-with-pandas/
推荐阅读
- python - Python如何在Docker中接收SIGINT以停止服务?
- javascript - (Svelte 3)我可以将图像放在使用它们的组件附近吗?
- python - 烧瓶邮件连接重置错误:[WinError 10054]
- c# - 扩展方法 Average() C#
- python - 树视图标题的活动背景颜色
- html - 无法在特定分辨率下折叠引导导航栏
- python - 硒驱动程序中的Python递增字符串编号
- python - 如何使用递归处理可变数量的嵌套for循环?
- java - 通过 Spring Data JPA 请求子列表的子条件
- javascript - 根据值将数组收集到子数组桶中(本身就是数组)