Skip to content

Instantly share code, notes, and snippets.

@Kcrong
Created July 7, 2017 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcrong/7525fe7e22ad1293ac74ced9a452b8d6 to your computer and use it in GitHub Desktop.
Save Kcrong/7525fe7e22ad1293ac74ced9a452b8d6 to your computer and use it in GitHub Desktop.
Excel to dict
import json
import pandas as pd
from collections import defaultdict
multi_depth_defaultdict = lambda: defaultdict(multi_depth_defaultdict)
df = pd.read_excel('category.xlsx')
data = multi_depth_defaultdict()
for idx, df_data in df.iterrows():
d1, d2, d3, d4 = [_.strip() for _ in df_data]
data[d1][d2][d3] = d4
with open('category_dump.json', 'w') as f:
json.dump(data, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment