Dictionaries are a very useful element in python to manage your data.
I often save and load dictionaries to a file to use as save and load systems in my tools.
The example underneath shows how to save a dictionary to a text file with json.dumps() and loading it back in using json.load()
Save a dictionary to a file
import json dict = {"hello" : "dictionary", "this" : "is", "awesome" : {"sub" : "collection"}} dictString= json.dumps(dict, sort_keys=False ,ensure_ascii=True ,indent=2) fs = open("c:/dict.txt","w") fs.write(dictString) fs.close()
Loads a dictionary from a file
import json fs = open("c:/dict.txt") dict = json.load(fs) fs.close() print dict
Geen opmerkingen:
Een reactie posten