Python json输出,包含不带键映射的行

Python json输出,包含不带键映射的行,python,json,Python,Json,我有一个json示例,我不确定将columns部分中的“url”和“failedCount”映射到rows部分中的每一行,然后将所有内容输出到csv中的最佳方式是什么,这样看起来就像这样 url,failedCount https://stefanolsen.com/favicon.ico, 663 https://stefanolsen.com/apple-touch-icon.png, 282 https://stefanolsen.com/apple-touch-icon-precompo

我有一个json示例,我不确定将columns部分中的“url”和“failedCount”映射到rows部分中的每一行,然后将所有内容输出到csv中的最佳方式是什么,这样看起来就像这样

url,failedCount
https://stefanolsen.com/favicon.ico, 663
https://stefanolsen.com/apple-touch-icon.png, 282
https://stefanolsen.com/apple-touch-icon-precomposed.png, 282
json示例:

{
    "tables":[{
        "name":"Primary",
        "columns":[
        {
            "name":"url",
            "type":"string"
        },
        {
            "name":"failedCount",
            "type":"long"
        }],
        "rows":[
            ["https://stefanolsen.com/favicon.ico", 663],
            ["https://stefanolsen.com/apple-touch-icon.png", 282],
            ["https://stefanolsen.com/apple-touch-icon-precomposed.png", 282]

        ]
    }]
}

试图使用源代码,但运气不佳

您只需从JSON数据中获取
'rows'
键,它已经是您需要的格式(列表/元组的列表/元组)

然后
test.csv
包含

url,failedCount
https://stefanolsen.com/favicon.ico,663
https://stefanolsen.com/apple-touch-icon.png,282
https://stefanolsen.com/apple-touch-icon-precomposed.png,282

不要在逗号和数字之间添加空格,因为这将使解析csv文件变得非常简单。

您需要做的就是从JSON数据中获取
'rows'
键,它已经是您需要的格式(即列表/元组的列表/元组)

然后
test.csv
包含

url,failedCount
https://stefanolsen.com/favicon.ico,663
https://stefanolsen.com/apple-touch-icon.png,282
https://stefanolsen.com/apple-touch-icon-precomposed.png,282

不要在逗号和数字之间添加空格,因为这将使解析csv文件变得非常简单。

您可以这样做吗

import csv

with open('output.csv', 'w+', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['url', 'failedCount'])
    for table in data['tables']:
        for row in table['rows']:
            writer.writerow([row[0], row[1]])

你能做这样的事吗

import csv

with open('output.csv', 'w+', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['url', 'failedCount'])
    for table in data['tables']:
        for row in table['rows']:
            writer.writerow([row[0], row[1]])

在查看了示例JSON输入的内容之后,我认为最好的方法是编写一些“数据驱动”的内容,即列名没有硬编码到其中。这意味着,如果他们有不同的名字和/或数量不同,就不需要更改

import csv
import json

json_sample = '''
{
    "tables":[{
        "name":"Primary",
        "columns":[
        {
            "name":"url",
            "type":"string"
        },
        {
            "name":"failedCount",
            "type":"long"
        }],
        "rows":[
            ["https://stefanolsen.com/favicon.ico", 663],
            ["https://stefanolsen.com/apple-touch-icon.png", 282],
            ["https://stefanolsen.com/apple-touch-icon-precomposed.png", 282]

        ]
    }]
}'''

filename = 'converted_json.csv'
json_obj = json.loads(json_sample)  # Deserialize.

table = json_obj['tables'][0]  # First table.
fields = [col['name'] for col in table['columns']]  # Extract table field names.

with open(filename, 'w', newline='') as outp:  # Create csv file from table.
    writer = csv.writer(outp)
    writer.writerow(fields)
    writer.writerows(table['rows'])

print('done')


在查看了示例JSON输入的内容之后,我认为最好的方法是编写一些“数据驱动”的内容,即列名没有硬编码到其中。这意味着,如果他们有不同的名字和/或数量不同,就不需要更改

import csv
import json

json_sample = '''
{
    "tables":[{
        "name":"Primary",
        "columns":[
        {
            "name":"url",
            "type":"string"
        },
        {
            "name":"failedCount",
            "type":"long"
        }],
        "rows":[
            ["https://stefanolsen.com/favicon.ico", 663],
            ["https://stefanolsen.com/apple-touch-icon.png", 282],
            ["https://stefanolsen.com/apple-touch-icon-precomposed.png", 282]

        ]
    }]
}'''

filename = 'converted_json.csv'
json_obj = json.loads(json_sample)  # Deserialize.

table = json_obj['tables'][0]  # First table.
fields = [col['name'] for col in table['columns']]  # Extract table field names.

with open(filename, 'w', newline='') as outp:  # Create csv file from table.
    writer = csv.writer(outp)
    writer.writerow(fields)
    writer.writerows(table['rows'])

print('done')


感谢您使用不带硬编码列名的方法感谢您使用不带硬编码列名的方法