xxxxxxxxxx
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
xxxxxxxxxx
import json
data = {}
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
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
# 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
# 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
# Python program to write JSON
# to a file
import json
# Data to be written
dictionary ={
"name" : "sathiyajith",
"rollno" : 56,
"cgpa" : 8.6,
"phonenumber" : "9976770500"
}
with open("sample.json", "w") as outfile:
json.dump(dictionary, outfile)