Advertisement
Guest User

Untitled

a guest
Aug 10th, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import csv
  2.  
  3. # Load the CSV
  4. inv = []
  5. with open("/home/trapezoid/code/foo/inv") as f:
  6. csv_r = csv.DictReader(f, fieldnames=["County", "Serial", "Model", "Type"], quotechar='"', quoting=csv.QUOTE_ALL)
  7. for r in csv_r:
  8. inv.append(r)
  9.  
  10. # Create a list of just county names
  11. all_county_names = [r['County'] for r in inv]
  12.  
  13. # Create a dict to hold the data as we iterate through the county hardware
  14. county_data = {}
  15.  
  16. # Iterate through each county, and create a nested dictionary. Top level dict keys are the county names, the nested dict is a map of hardware type to hardware count
  17. for county in all_county_names:
  18. county_inv = [r for r in inv if r['County'] == county]
  19. county_counts = defaultdict(int)
  20. for x in county_inv:
  21. if not x['Type']:
  22. x['Type'] = ''
  23. # Making things easier by concatenating the HW model to HW type, since the same HW can sometimes be used for different things (i.e., ADJ clients and EMS clients)
  24. county_counts[x['Model'] + '-' + x['Type']] += 1
  25. county_data[county] = county_counts
  26.  
  27. # Create an empty list to hold the names of any counties that match the hardware in Mesa
  28. full_matches = []
  29.  
  30. # Iterate through the county data dict, and compare the HW types for each county to the HW types used in Mesa. If there's an match, add the county name to the full_matches list
  31. for k,v in county_data.items():
  32. matches = []
  33. for kk, vv in v.items():
  34. if kk in county_data['Mesa'].keys():
  35. matches.append(kk)
  36. if matches == list(county_data['Mesa'].keys()):
  37. full_matches.append(k)
  38.  
  39. print(full_matches)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement