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
  bool b = (num == 100) this doesn't work? MelonMusk 2 150 Jun-12-2020, 02:18 AM
Last Post: bowlofred
  len() function, numbers doesn't work with Geany Editor Penguin827 3 268 May-08-2020, 04:08 AM
Last Post: buran
  import scalalib package doesn't work manu_brighter 2 263 Apr-17-2020, 06:36 PM
Last Post: snippsat
  Why my lambda doesn't work properly? Snake 6 500 Mar-29-2020, 04:26 PM
Last Post: Snake
  Multiprocessing doesn't seem to work? t4keheart 10 493 Feb-13-2020, 06:51 PM
Last Post: t4keheart
  why my function doesn't work cimerio 4 453 Jan-20-2020, 08:11 PM
Last Post: cimerio
  Doesn't work function pyautogui.typewrite() aliyevmiras 1 581 Dec-22-2019, 11:35 AM
Last Post: aliyevmiras
  I don't understand why this doesn't work sandeen 4 431 Dec-19-2019, 07:52 PM
Last Post: sandeen
  For loop in my __init__ doesn't work as expected Jessy 2 307 Nov-18-2019, 10:07 AM
Last Post: buran
  Python: command “python -m pip install --upgrade pip” doesn't work apollo 2 1,049 Sep-16-2019, 03:11 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)