I would like to write file with python
My code is producing:
{ date: 2018-11-28, weight: 172.91, height: 177.65, id:310 }
I want to have the following is the input inside the file
{ date: '2018-11-28', weight: 172.91, height: 177.65, id:310 }
With the single quote mark which I need it to be there.
Any help please for writing string into file with single quote mark '.
my code:
my_dict = { 'date': '2018-11-28', 'weight': 172.91, 'height': 177.65, 'id':310 }
my_dict = str(my_dict)
f = open('file.txt', 'w')
f.write(my_dict)
f.close()
Thanks
Use the JSON library. In your case you would use:
with open('file.txt', 'w') as f:
json.dump(my_dict, f, indent=4)
Or, to mimic your approach to files:
f = open('file.txt', w)
json.dump(my_dict, f, indent=4)
f.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.