Created
April 27, 2024 05:58
-
-
Save yeiichi/b0c7d634343c4debf007fc3258e89cf0 to your computer and use it in GitHub Desktop.
Convert an n*2 array (CSV) to a JSON file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
from io import StringIO | |
import pandas as pd | |
def csv_to_json(csv_in): | |
"""Convert an n*2 array (CSV) to a JSON file. | |
""" | |
# Load CSV data | |
df = pd.read_csv(csv_in, usecols=['category', 'subcategory']).dropna() | |
cat_sub_dict = {} # Initialize category-subcategory dictionary. | |
cats_already_added = set() # Initialize checklist (check-set) | |
for rec in df.to_dict(orient='records'): | |
cat = rec['category'] | |
sub = rec['subcategory'] | |
# Category(cat) appears for the first time | |
if cat not in cats_already_added: | |
cat_sub_dict[cat] = [sub] | |
# If the key 'cat' is already defined in the dictionary. | |
else: | |
cat_sub_dict[cat].append(sub) | |
cats_already_added.update(cat) # Update the checklist. | |
# https://qiita.com/Hyperion13fleet/items/7129623ab32bdcc6e203 | |
out_file = 'sample.json' | |
with open(out_file, 'w') as f: | |
json.dump( | |
cat_sub_dict, f, | |
ensure_ascii=False, | |
indent=2, | |
sort_keys=True | |
) | |
print(f'Saved: {out_file}') | |
if __name__ == '__main__': | |
pseudo_csv = StringIO('category,subcategory\nA,p\nA,q\nA,r\nB,x\nB,y\nB,z') | |
csv_to_json(csv_in=pseudo_csv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment