Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import json
  2. import csv
  3. from collections import defaultdict
  4.  
  5. def build_dict(source_file):
  6.     projects = defaultdict(dict)
  7.     headers = ["Name","Level","100","551", "301", "302", "303", "304", "305", "306", "307", "308", "309", "310", "401", "402", "403", "404", "405", "406", "407", "408", "501", "502", "503", "504", "505", "506", "552", "553", "554", "555", "556", "507", "557", "508", "509", "602", "603", "604", "605", "601", "606", "607", "608", "609", "610", "111", "112", "113", "211", "212", "213", "121", "122", "123", "221", "222", "223", "131", "132", "133", "231", "232", "233", "141", "142", "143", "241", "242", "243", "151", "152", "153", "251", "252", "253"]
  8.     with open(source_file, 'r') as fp:
  9.         reader = csv.DictReader(fp, fieldnames=headers, dialect='excel',skipinitialspace=True)
  10.         i = 0
  11.         for rowdict in reader:
  12.             if None in rowdict:
  13.                     del rowdict[None]
  14.             name = rowdict.pop("Name")
  15.             level = rowdict.pop("Level")
  16.             rowdict = {k:int(v) for k,v in rowdict.items() if v != ""}
  17.             projects[i] = {"Name":name}
  18.             projects[i][level] = rowdict
  19.             i+=1
  20.     return dict(projects)
  21.  
  22. source_file = 'Data.csv'
  23. oldDict = build_dict(source_file)
  24. newDict = defaultdict(dict)
  25.  
  26. for j in range(0,910,9):
  27.     for k in range(j+1,j+9,1):
  28.         oldDict[k].pop("Name")  
  29.     name = oldDict[j].pop("Name")
  30.     newEntry = {"Name":name,"ID":'',"awakenLevel":{},"magiaLevel":{}}
  31.     for i in range(0,5,1):
  32.         newEntry["awakenLevel"].update(oldDict[j+i])
  33.     for i in range(0,4,1):
  34.         newEntry["magiaLevel"].update(oldDict[j+5+i])
  35.     newDict[j/9] = newEntry
  36.    
  37. jsonfile = open('Data.json', 'w')
  38. jsonfile.write('[\n')
  39. for yaBaby in newDict:
  40.     json.dump(newDict[yaBaby], jsonfile)
  41.     jsonfile.write(',\n')
  42. jsonfile.write(']')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement