用一个值分隔一个 CSV,里面有多个值

separating a CSV with a value with multiple values inside

目前正在尝试编写一个从 ZabbixAPI 密钥中提取数据的脚本。我有处理数据的脚本,然后将其转换为 CSV 文件。

我面临的问题是,在它被转换后,我定义的 headers 之一有多个值,我需要在其中作为单个 headers 并将值附加为它自己的列.

例如

{'hostid': '10084', 'host': 'Zabbix server', 'name': 'Zabbix server', 'status': '0', 'inventory': {'os_full': '', 'tag': '', 'location': '', 'location_lat': '', 'location_lon': ''}}

是代码,但是正如问题所说,'Inventory' 的值包含我需要在其自己的列中的所有数据。

如何分隔值才能正确显示?

这是完整的脚本

import requests
import json
import pandas as pd
import csv


url = 'http://XXX/api_jsonrpc.php' 

payload = '{"jsonrpc": "2.0", "method": "host.get", "params": {"output": ["hostid","host","name","status","location","location_lat","location_lon"],"selectInventory": ["os_full","tag","location","location_lat","location_lon"]}, "auth": "XXX", "id": 1 }'
headers = {'content-type': 'application/json-rpc'}
r = requests.post(url, data=payload, headers=headers, )
hostslist = r.json()['result']

print(type(hostslist))
print(hostslist)



file = open('hostInventory.csv', 'w+', newline='')

with file:

    header = ['hostid', 'host', 'name', 'status', 'inventory']
    writer = csv.DictWriter(file, fieldnames = header)

    writer.writeheader()
    writer.writerows(hostslist)

先修改dicts,再照原样写。示例:

for host in hostlist:
    host['os_full'] = host['inventory']['os_full']
    del host['inventory']

with open('hostInventory.csv', 'w+', newline=''):

    header = ['hostid', 'host', 'name', 'status', 'os_full']
    writer = csv.DictWriter(file, fieldnames = header)

    writer.writeheader()
    writer.writerows(hostslist)