简体   繁体   English

使用 PYTHON 读取 JSON 文件并使用该数据创建一个 excel 文件

[英]Read JSON file using PYTHON and create an excel file with that data

I am working on a project where i have to read a JSON file and then load the data in an excel file.我正在做一个项目,我必须读取一个 JSON 文件,然后将数据加载到一个 excel 文件中。 Not sure how to go about this in Python and i need some real help.不知道如何在 Python 中解决这个问题,我需要一些真正的帮助。 Here is the JSON structure:这是JSON结构:

{"Models":[{"name":"AAA", "text":"some text", "structure":[{"column":"zzz", "type":"string"}, .....]}, {"name":"BBB", "text":"some text", "structure":[{"column":"zzz", "type":"string"}, .....]}]}

I need to convert this in the following excel format: Name|我需要将其转换为以下 excel 格式:名称| Column |专栏 | Type AAA |类型AAA | zzz | zzz | string BBB |字符串 BBB | yyy | yyy | text CCC |文字CCC | xxx | xxx | string细绳

Not sure how to do this as Column and Type are under a different key and Name is different key.不确定如何执行此操作,因为 Column 和 Type 在不同的键下,而 Name 是不同的键。

Load json file as dict and iterate through it将 json 文件加载为 dict 并遍历它

import json

f = open("f.json")
data = json.load(f)
f.close()
for x in data.get('Models'):
    name = x.get('name')
    for s in x.get('structure'):
        #write this string to a csv file
        print(name + ' ' + s.get('column') + ' ' + s.get('type'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM