I am using the python csv module and I have a CSV with 3 columns, Item, Part, Category.
I'd like to create a dict that combines all the categories and then sorts their values with the Item:Part.
For Example:
512 SSD SATA,42-000153,Hardware
5M DisplayPort 1.2 Cable,42-000135,Cable
90W AC Adapter,42-000146,Adapter
4 port USB hub,42-000126,Adapter
The result I'm getting is:
mydict = {
Hardware:{512 SSD SATA:42-000153},
Cable:{5M DisplayPort 1.2 Cable,42-000135},
Adapter:{90W AC Adapter:42-000146},
Adapter:{4 port USB hub:42-000126}
}
This almost gets me there:
def build_dict(source_file):
projects = defaultdict(dict)
headers = ['Product', 'Part Number', 'Category']
with open(source_file, 'rb') as fp:
reader = csv.DictReader(fp, fieldnames=headers, dialect='excel',
skipinitialspace=True)
for rowdict in reader:
if None in rowdict:
del rowdict[None]
category = rowdict.pop("Category")
projects[category] = rowdict
return dict(projects)
source_file = 'test.csv'
The Result I'm looking for:
mydict = {
Hardware:{512 SSD SATA:42-000153},
Cable:{5M DisplayPort 1.2 Cable,42-000135},
Adapter:{90W AC Adapter:42-000146,4 port USB hub:42-000126}
}
Please Help!