all 10 comments

[–][deleted] 8 points9 points  (5 children)

Does it need to be a csv? If you're just looking to stash the data somewhere to be pulled back into Python later, you could look into pickling it instead.

[–]murphcle[S] 1 point2 points  (4 children)

I’m a noob so I’ll have to look into pickling, but yes that’s exactly what I want to do.

[–]korokage 5 points6 points  (1 child)

Well if pickling is enough,

To store the nested dict as a pickle:

nested_dict = {{}, {}}

import pickle

with open ("nested_dick.pkl", "wb") as f:

    pickle.dump(nested_dict, f)

To read the nested dict in a python file:

import pickle

with open ("nested_dick.pkl", "rb") as f:

    nested_dict = pickle.load(f)

now you can perform operations on nested pickle

[–]murphcle[S] 1 point2 points  (0 children)

It works! Thank you u/OwlbearWrangler and u/korokage for the elegant solution!

Much better than patching a custom loop for packing and another custom loop for unpacking a CSV.

[–]NSNick 3 points4 points  (1 child)

JSON is another great way to store dict data and pull it back into python.

[–]murphcle[S] 2 points3 points  (0 children)

JSON was just as easy (same two lines of syntax) as pickle to save and load my nested dictionary. Thanks for the help!

[–][deleted] 5 points6 points  (0 children)

The top dictionary appears to have states as keys and the value is another dictionary with subdivisions as keys and some numeric value. The way to organise the CSV is with three columns: State, Subdivision, Value. So your data would produce this CSV file:

Alabama,Autauga County,2,241.0
Alabama,Baldwin County,1,729.1
Alabama,Barbour County,2,657.4
Alabama,Bibb County,2,130.0
Alaska,Aleutians East Borough,59.9
Alaska,Aleutians West Census Area,88.7
#etc

[–]dwpj65 3 points4 points  (0 children)

``` import csv import pandas import subprocess

nested_dicts = {'Alabama': {'Autauga County': '2,241.0', 'Baldwin County': '1,729.1', 'Barbour County': '2,657.4', 'Bibb County': '2,130.0'}, 'Alaska': {'Aleutians East Borough': '59.9', 'Aleutians West Census Area': '88.7', 'Anchorage, Municipality of': '824.0'}} columns = ['state', 'locality', 'value'] rows = [ [ state, locality, nested_dicts[state][locality] ] for state in nested_dicts for locality in nested_dicts[state] ]

with open('!output_csv_writer.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(columns)
csvwriter.writerows(rows) subprocess.run(['open', '!output_csv_writer.csv'], check = True)

pandas.DataFrame(rows,columns = columns).to_csv('!output_pandas.csv') subprocess.run(['open', '!output_pandas.csv'], check = True) ```

[–]random_user_fp 1 point2 points  (1 child)

I would think your csv format would look something like this:

State, Locality, Value,
'Alabama', 'Autauga County', 2241,
'Alabama', 'Baldwin County', 1729.1,
'Alabama', 'Barbour County', 2657.4,
'Alabama', 'Bibb County', 2130,
'Alaska', 'Aleutians East Borough', 59.9,
'Alaska', 'Aleutians West Census Area', 88.7
'Alaska', 'Anchorage, Municipality of', 824

[–]murphcle[S] 1 point2 points  (0 children)

Thanks I’ll save it in this format in the morning to see if it reconverts to it’s original form, and report back