Python Forum
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]}
Reply
#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
Reply
#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 . . .
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,679 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 838 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,655 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,050 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,929 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,552 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,888 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,636 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020