Excel到JSON的转换

2024-06-16 18:42:30 发布

您现在位置:Python中文网/ 问答频道 /正文

Thisexcel文件已转换为json, 但在python中使用pandas库时,它会按列执行转换,并显示为this。 我希望它以以下格式显示:

{"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....

] }

请不要考虑这个问题的上下文,我知道它是跛脚的,但是我在做一些不同的事情。


Tags: orandofthenamesoenglishon
1条回答
网友
1楼 · 发布于 2024-06-16 18:42:30

.csv的结构可以在python中显示为字典。作为一个文件,您可以使用nested.json或.xml来实现此目的

.csv中的嵌套.json可以像在csv to nested JSON?中一样使用csv.DictReader生成

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

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

另请参见:How can I load a CSV file into a QTreeView?

How to transform an XML file using XSLT in Python?

相关问题 更多 >