xml - How can I edit a JSON file using Python? -
i've looked @ several questions , made appropriate changes, python script still isn't working. still new python apologize if i'm making simple mistake...
i'm sending xml response webpage display value json file. tested xml portion of script , works fine - when add code edit json file, xml response doesn't load. here's i've got - doing wrong?
#!/usr/bin/env python import cgi import cgitb import json open('../../../var/www/data.json', 'r+') f: json_data = json.load(f) json_data[0]['a'] = '7' f.seek(0) f.write(json.dumps(json_data)) f.truncate() open('../../../var/www/data.json') data_file: data = json.load(data_file) cgitb.enable() print "content-type: text/xml" print # blank line, end of headers print "<?xml version='1.0' encoding='utf-8' ?><inputs><data>"+data[0]["a"]+"</data></inputs>"
i tried this...but didn't work either:
#!/usr/bin/env python import cgi import cgitb import json open('../../../var/www/data.json', 'r') f: json_data = json.load(f) json_data[0]["a"] = "7" open('../../../var/www/data.json', 'w') f f.write(json.dumps(json_data)) open('../../../var/www/data.json') data_file: data = json.load(data_file) cgitb.enable() print "content-type: text/xml" print # blank line, end of headers print "<?xml version='1.0' encoding='utf-8' ?><inputs><data>"+data[0]["a"]+"</data></inputs>"
and here json file:
[ { "a": "11", "b": "4", "c": "", "d": "basement", "e": "digital", "f": "", "g": "", "h": "", "i": "", "j": "", "k": "", "l": "", "m": "", "n": "on", "o": "off", "p": "1" }, { "a": "11", "b": "3", "c": "", "d": "silver", "e": "digital", "f": "", "g": "", "h": "", "i": "", "j": "", "k": "", "l": "", "m": "", "n": "on", "o": "off", "p": "1" }, { "a": "12", "b": "3", "c": "", "d": "bench", "e": "digital", "f": "", "g": "", "h": "", "i": "", "j": "", "k": "", "l": "", "m": "", "n": "on", "o": "off", "p": "1" } ]
you should include error/console output next time.
in second file:
- line 11 missing
:
@ end of line
Comments
Post a Comment