**edit: I need the key:value pairs of the printers data to go under a "topic/flag" of that printers name.
I am trying to get the output of wmic printer list brief
and turn it into a JSON file for storage and parsing at a later date. So far I have gotten it into CSV format with the /format:csv
flag and have gotten it where I can go through and turn that CSV data into a list. The problem is the formatting of the json. How can I format the json where all of the data is under the 'flag' of 'Name'? I know that I am not explaining this like I should be but I am stumped. Here is an example of what I want it to look like,
{
"Adobe PDF": {
"Node": "____________",
"Location": "____________",
"PrinterState": "____________",
"PrinterStatus": "____________",
"ShareName": "____________",
"SystemName": "____________"
}
"OneNote for Windows 10": {
"Node": "____________",
"Location": "____________",
"PrinterState": "____________",
"PrinterStatus": "____________",
"ShareName": "____________",
"SystemName": "____________"
}
}
On another note how can I clean up and compress this python code to make it more efficient and take up less space?
Python Code:
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)
Starting 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
Example List:
['Node', 'COMPUTERNAME', 'COMPUTERNAME', 'COMPUTERNAME', 'COMPUTERNAME', 'COMPUTERNAME','COMPUTERNAME', 'COMPUTERNAME']
CodePudding user response:
Read the csv file, with csv.DictReader
. Create a dict and dump that dict into JSON. The dict
will become JSON Object.
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)
the output 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"
}
}