Dec-05-2016, 10:56 PM
(This post was last modified: Dec-05-2016, 10:57 PM by PickyBiker.)
I am learning python and having a problem with reading .json files. There are 2 reads, one for status.json and the other for schedue.json
The code shown below works just fine, unless you un-comment the last two lines.
Why is the second read causing errors?
Here is the schedule.json file contents:
The code shown below works just fine, unless you un-comment the last two lines.
Why is the second read causing errors?
import os, sys, time, datetime, json def WriteStatusJSON(control, pump, zones, ontime): update = {"control": control, "pump": pump, "zones": zones, "ontime": ontime} with open('status.json', 'w') as outfile: json.dump(update, outfile) outfile.close() def ReadStatusJSON(): with open('status.json') as infile: data = json.load(infile) infile.close() control = data["control"] zones = data["zones"] pump = data["pump"] ontime = data["ontime"] if ontime != None: hour = ontime[0] minute = ontime[1] return data def WriteScheduleJSON(weekdays, ontime, zones): update = {"weekdays": weekdays, "ontime": ontime, "zones": zones} with open('schedule.json', 'w') as outfile: json.dump(update, outfile) outfile.close() def ReadScheduleJSON(): with open('schedule.json') as infile: data = json.load(infile) infile.close() weekdays = data["weekdays"] ontime = data["ontime"] hour = ontime[0] minute = ontime[1] zones = data["zones"] return data status = ReadStatusJSON() print(status['control']) #schedule = ReadScheduleJSON() #print(schedule['weekdays'])Here is the status.json file contents:
Quote:{"control": "Start", "zones": [[5, 15]], "pump": false, "ontime": [13, 25]}
Here is the schedule.json file contents:
Quote:{"zones": [[1, 10], [2, 10], [3, 10], [4, 10], [5, 10], [6, 10]], "weekdays": [1, 3, 5], "ontime": [5, 30]}