open and save data in json fil code example
Example 1: how to save to file in python
with open('data.txt', 'w') as my_data_file:
my_data_file.write(WhateverYourInputIs)
Example 2: write json dump to file python
import json
import numpy as np
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
json.dumps(data, cls=NpEncoder)