Created
September 21, 2012 13:38
-
-
Save billroy/3761495 to your computer and use it in GitHub Desktop.
Read/write JSON object from file in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simplejson | |
import json | |
def put(data, filename): | |
try: | |
jsondata = simplejson.dumps(data, indent=4, skipkeys=True, sort_keys=True) | |
fd = open(filename, 'w') | |
fd.write(jsondata) | |
fd.close() | |
except: | |
print 'ERROR writing', filename | |
pass | |
def get(filename): | |
returndata = {} | |
try: | |
fd = open(filename, 'r') | |
text = fd.read() | |
fd.close() | |
returndata = json.read(text) | |
# Hm. this returns unicode keys... | |
#returndata = simplejson.loads(text) | |
except: | |
print 'COULD NOT LOAD:', filename | |
return returndata | |
if __name__ == '__main__': | |
o = get(sys.argv[1]); | |
if o: | |
put(o, sys.argv[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice, thakns a lot