Created
January 16, 2018 15:49
-
-
Save JasonCrowe/14844d70ce97f3aea65ab22b0a9bda72 to your computer and use it in GitHub Desktop.
Read/Write data to/from json files
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
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