Skip to content

Instantly share code, notes, and snippets.

@JasonCrowe
Created January 16, 2018 15:49
Show Gist options
  • Save JasonCrowe/14844d70ce97f3aea65ab22b0a9bda72 to your computer and use it in GitHub Desktop.
Save JasonCrowe/14844d70ce97f3aea65ab22b0a9bda72 to your computer and use it in GitHub Desktop.
Read/Write data to/from json files
import json
from glob import glob
def save_data(data):
try:
asin = data['asin']
file_loc = 'data/{}.json'.format(asin)
file = open(file_loc, 'w+')
json.dump(data, file)
except KeyError:
pass
def read_json_files():
output_list = []
for f in glob('data/*json'):
file = open(f, 'r')
output_list.append(json.load(file))
return output_list
"""
USAGE EXAMPLES
for i in range(25):
x = dict(test = 'yes', name='Jason', asin='xyz123{}'.format(i), count=i)
save_data(x)
print read_json_files()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment