Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Why doesn't this work?
#1
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?

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]}
Quote
#2
What's the error?

Also, why are you closing your files? with does that for you. If you want to close things yourself, don't use with :p
Quote
#3
I didn't know with closed the files... I'll change that, THANKS!

Here are the errors:
Error:
Auto Traceback (most recent call last):  File "crash.py", line 41, in <module>    schedule = ReadScheduleJSON()  File "crash.py", line 30, in ReadScheduleJSON    data = json.load(infile)  File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 268, in load    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)  File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 319, in loads    return _default_decoder.decode(s)  File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 339, in decode    obj, end = self.raw_decode(s, idx=_w(s, 0).end())  File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 357, in raw_decode    raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ------------------ (program exited with code: 1) Press any key to continue . . .
Quote
#4
I opened schedule.json with a hex editor and found there were 3 unprintable characters in the file before the opening brace '{'. Once removed, the file works just fine.

Now to find out why those get in there, but that's a different problem.

Case closed.
nilamo likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  why my function doesn't work cimerio 3 112 Nov-20-2019, 01:13 PM
Last Post: ChislaineWijdeven
  For loop in my __init__ doesn't work as expected Jessy 2 94 Nov-18-2019, 10:07 AM
Last Post: buran
  Python: command “python -m pip install --upgrade pip” doesn't work apollo 2 263 Sep-16-2019, 03:11 PM
Last Post: snippsat
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 368 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Python Code doesn't work! the_entrepreneur 5 476 Apr-20-2019, 08:05 AM
Last Post: SheeppOSU
  New to MySQL, can't work out why code doesn't work kintarowonders 6 556 Feb-18-2019, 05:23 PM
Last Post: kintarowonders
  This doesn't work like in book Blaedel 1 381 Jan-14-2019, 05:41 PM
Last Post: metulburr
  Why doesn't strip work? cybervigilante 5 754 Dec-27-2018, 02:21 AM
Last Post: metulburr
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 1,089 Nov-03-2018, 06:53 PM
Last Post: j.crater
  len() function doesn't work with Geany Editor hudabaig 2 930 Jun-01-2018, 11:20 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)