首页 > 解决方案 > 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....

] }

请不要考虑问题的上下文,我知道它很蹩脚,但我正在做一些不同的事情。

标签: pythonjsonexcelpandas

解决方案


.csv 的结构可以在 python 中显示为字典。作为文件,您可以为此使用嵌套的 .json或 .xml。

.csv 中的嵌套 .json 可以csv.DictReadercsv 中生成嵌套 JSON 吗?

.json是一种序列化格式,.csv 文件的结构是嵌套的 .json、字典或 .xml 的结构。要使用 pandas 生成 .xml,请使用模块进行.xml处理,例如xml.etree.ElementTree

https://stackabuse.com/reading-and-writing-xml-files-in-python-with-pandas/

另请参阅:如何将 CSV 文件加载到 QTreeView 中?

如何在 Python 中使用 XSLT 转换 XML 文件?


推荐阅读