I get a CSV from our developers that looks like this (example values only)
Category, Sub-Category, Template Name
Health Check,CPU,checkCPU
Health Check,Memory,checkMemory
Service Request,Reboot Device, rebootDevice
Service Request,Check CPU,checkCPU-SR
I need my python script to be able to read this (values will grow and change over time) and give me the Template Name for a given Category and Sub-Category. I can make this work by reading in the CSV and looping through it, searching for the values I want, but it seems like there has to be an easier way.
If I load a JSON file like this, I can use json.load to convert things to a dict, then easily retrieve the value I want without having to loop through things.
{
"Health Check": {
"CPU": "checkCPU",
"Memory": "checkMemory"
},
"Service Request": {
"Reboot Device": "rebootDevice",
"Check CPU": "checkCPU-SR"
}
}
Then I use something like
import json
import csv
with open('categories.json','r') as f:
myDict = json.load(f)
print(myDict["Health Check"]["CPU"])
I'd much rather use the dict method, but I don't know if there's a way to achieve this from a CSV file. I've tried a few things like csv.dictreader or Pandas, but I can't get either of them to work. Pandas I could setup a key, but none of the values here are unique. With csv.dictreader, the nested fashion of this data (multiple keys/values under a single heading like Health Check) don't seem to work.