Created
May 18, 2019 21:34
-
-
Save ludovikcoba/66af4ded474ea8673bafbede368c9e79 to your computer and use it in GitHub Desktop.
Revisions
-
ludovikcoba revised this gist
May 18, 2019 . No changes.There are no files selected for viewing
-
ludovikcoba created this gist
May 18, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ # Fix of https://gist.github.com/paulgb/5265767 import json import pandas as pd from glob import glob import sys def convert(x): ob = json.loads(x) new_ob = {} for k, v in ob.items(): if isinstance(v, list): new_ob[k] = ','.join(v) elif isinstance(v, dict): for kk, vv in v.items(): new_ob['%s_%s' % (k, kk)] = vv else: new_ob[k] = v return new_ob def main(filename): json_file = open(filename, "r", encoding="utf8") df = pd.DataFrame([convert(line) for line in json_file]) csv_filename = '%s.csv' % filename[:-5] df.to_csv(csv_filename) if __name__ == '__main__': if len(sys.argv) == 0: sys.exit("Target file is missing.") for i in range(1, len(sys.argv) + 1): main(sys.argv[i])