将多个列表组合成具有不同缩进级别的 JSON 文件

Combine Multiple Lists into a JSON file with different indention levels

**编辑:我需要 key:value 对打印机数据放在该打印机名称的“topic/flag”下。

我正在尝试获取 wmic printer list brief 的输出并将其转换为 JSON 文件以供日后存储和解析。到目前为止,我已经将其转换为带有 /format:csv 标志的 CSV 格式,并已将其转换为可以浏览并将该 CSV 数据转换为列表的位置。问题是 json 的格式。如何格式化 json,其中所有数据都在 'Name' 的 'flag' 下?我知道我没有像我应该的那样解释这个,但我很难过。这是我希望它看起来像的示例,

{
    "Adobe PDF": {
        "Node": "____________",
        "Location": "____________",
    "PrinterState": "____________",
    "PrinterStatus": "____________",
    "ShareName": "____________",
    "SystemName": "____________"
    }
    "OneNote for Windows 10": {
        "Node": "____________",
        "Location": "____________",
    "PrinterState": "____________",
    "PrinterStatus": "____________",
    "ShareName": "____________",
    "SystemName": "____________"
    }
}

另外请注意,我如何清理和压缩此 python 代码以使其更高效并占用更少 space?

Python代码:

import csv
import json

Node = []
Location = []
Name = []
PrinterState = []
PrinterStatus = []
ShareName = []
SystemName = []


csvfile = getprinters()
reader = [row for row in csv.reader(csvfile.decode('utf-8').splitlines())]
for i in reader:
if i == []:
    continue
else:
    item1 = i[0]
    item2 = i[1]
    item3 = i[2]
    item4 = i[3]
    item5 = i[4]
    item6 = i[5]
    item7 = i[6]
    Node.append(item1)
    Location.append(item2)
    Name.append(item3)
    PrinterState.append(item4)
    PrinterStatus.append(item5)
    ShareName.append(item6)
    SystemName.append(item7)

开始 CSV:

Node,Location,Name,PrinterState,PrinterStatus,ShareName,SystemName
COMPUTERNAME,,Adobe PDF,0,3,,COMPUTERNAME
COMPUTERNAME,,OneNote for Windows 10,0,3,,COMPUTERNAME
COMPUTERNAME,,OneNote (Desktop),0,3,,COMPUTERNAME
COMPUTERNAME,,Microsoft XPS Document Writer,0,3,,COMPUTERNAME
COMPUTERNAME,,Microsoft Print to PDF,0,3,,COMPUTERNAME
COMPUTERNAME,http://12.345.6.789:0000/,HP###### (HP Officejet Pro 8610),0,3,,COMPUTERNAME
COMPUTERNAME,,Fax,0,3,,COMPUTERNAME

示例列表:

['Node', 'COMPUTERNAME', 'COMPUTERNAME', 'COMPUTERNAME', 'COMPUTERNAME', 'COMPUTERNAME','COMPUTERNAME', 'COMPUTERNAME']

读取csv文件,csv.DictReader。创建一个字典并将该字典转储到 JSON。 dict 将成为 JSON 对象。

import csv
import json

with open('start.csv') as f:
    rdr =  csv.DictReader(f)
    data = {row.get('Name'):row for row in rdr} # use row.pop('Name') if you want to remove Name key from row

with open('export.json', 'w') as f:
    json.dump(data, f, indent=4)

输出export.json:

{
    "Adobe PDF": {
        "Node": "COMPUTERNAME",
        "Location": "",
        "Name": "Adobe PDF",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    },
    "OneNote for Windows 10": {
        "Node": "COMPUTERNAME",
        "Location": "",
        "Name": "OneNote for Windows 10",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    },
    "OneNote (Desktop)": {
        "Node": "COMPUTERNAME",
        "Location": "",
        "Name": "OneNote (Desktop)",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    },
    "Microsoft XPS Document Writer": {
        "Node": "COMPUTERNAME",
        "Location": "",
        "Name": "Microsoft XPS Document Writer",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    },
    "Microsoft Print to PDF": {
        "Node": "COMPUTERNAME",
        "Location": "",
        "Name": "Microsoft Print to PDF",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    },
    "HP###### (HP Officejet Pro 8610)": {
        "Node": "COMPUTERNAME",
        "Location": "http://12.345.6.789:0000/",
        "Name": "HP###### (HP Officejet Pro 8610)",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    },
    "Fax": {
        "Node": "COMPUTERNAME",
        "Location": "",
        "Name": "Fax",
        "PrinterState": "0",
        "PrinterStatus": "3",
        "ShareName": "",
        "SystemName": "COMPUTERNAME"
    }
}