-
-
Save asw456/6035200 to your computer and use it in GitHub Desktop.
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
''' | |
Convert Yelp Academic Dataset from JSON to CSV | |
Requires Pandas (https://pypi.python.org/pypi/pandas) | |
By Paul Butler, No Rights Reserved | |
''' | |
import json | |
import pandas as pd | |
from glob import glob | |
def convert(x): | |
''' Convert a json string to a flat python dictionary | |
which can be passed into Pandas. ''' | |
ob = json.loads(x) | |
for k, v in ob.items(): | |
if isinstance(v, list): | |
ob[k] = ','.join(v) | |
elif isinstance(v, dict): | |
for kk, vv in v.items(): | |
ob['%s_%s' % (k, kk)] = vv | |
del ob[k] | |
return ob | |
for json_filename in glob('*.json'): | |
csv_filename = '%s.csv' % json_filename[:-5] | |
print 'Converting %s to %s' % (json_filename, csv_filename) | |
df = pd.DataFrame([convert(line) for line in file(json_filename)]) | |
df.to_csv(csv_filename, encoding='utf-8', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason this code works for all of the json files except the user one. Any ideas? Here is the error msg:
TypeError Traceback (most recent call last)
C:\Files\Masters\455\Yelp\convert4.py in ()
29
30 for fname in FILES:
---> 31 df = pd.DataFrame([convert(line) for line in file('%s.json' % fname)])
32 df.to_csv('%s.csv' % fname, encoding='utf-8')
C:\Files\Masters\455\Yelp\convert4.py in convert(x)
21 for k, v in ob.items():
22 if isinstance(v, list):
---> 23 ob[k] = ','.join(v)
24 elif isinstance(v, dict):
25 for kk, vv in v.items():
TypeError: sequence item 0: expected string, int found