xxxxxxxxxx
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
xxxxxxxxxx
import json
with open('data.json', 'w') as f:
json.dump(data, f)
xxxxxxxxxx
# to write on file
# data_dict is a dictionary
import json
with open('data.json', 'w') as f:
json.dump(data_dict, f)
xxxxxxxxxx
with open('output_name.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
xxxxxxxxxx
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
xxxxxxxxxx
On a modern system (i.e. Python 3 and UTF-8 support), you can write a nice file with:
import json
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
xxxxxxxxxx
import json
with open('data.txt') as json_file:
data = json.load(json_file)
for p in data['people']:
print('Name: ' + p['name'])
print('Website: ' + p['website'])
print('From: ' + p['from'])
print('')
xxxxxxxxxx
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)