简体   繁体   中英

Getting an error trying to json.load Python

I am trying to make some program to tell me which lesson I have at a certain time and day. To make it work, I put my schedule in a json file (the numbers correspond to the day number).

{
    "1": [
        "french",
        "french",
        "yearit",
        "geography",
        "hebrew",
        "shelah",
        "science",
        ""
    ],
    "2": [
        "sports",
        "literature",
        "math rotem",
        "geography",
        "hebrew",
        "hebrew",
        "science",
        "yearit"
    ],
    "3": [
        "yearit",
        "english",
        "math gila",
        "english",
        "yizhak",
        "yizhak",
        "",
        ""
    ],
    "4": [
        "english",
        "math gila",
        "science",
        "math rotem",
        "sports",
        "literature",
        "science",
        "science"
    ],
    "5": [
        "life skills",
        "yizhak",
        "french",
        "yizhak",
        "math gila",
        "italian",
        "italian",
        "math gila"
    ],
    "6": [
        "yizhak",
        "english",
        "english",
        "math gila",
        "science",
        "",
        "",
        ""
    ],
    "7": [
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "", 
    ],
}

I used this code to turn it into a python dictionary:

import json
with open('schedule.json', 'r') as f:
    schedule = json.load(f)
    
print(schedule)

But it gave me this error message:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\everything\coding lol\Python\CheckZoom\test.py", line 3, in <module>
    schedule = json.load(f)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 71 column 2 (char 721)

How can I solve this error? Any help appreciated.

As @buran pointed out, you have extra commas at the end of the file, specifically here:

    "7": [
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "", # this is an extra comma
    ], # this is an extra comma
}

But the file can still be processed by using the ast.literal_eval method :

import ast

with open('schedule.json', 'r') as f:
    text = f.read()
schedule = ast.literal_eval(text)
print(schedule)

Prints:

{'1': ['french', 'french', 'yearit', 'geography', 'hebrew', 'shelah', 'science', ''], '2': ['sports', 'literature', 'math rotem', 'geography', 'hebrew', 'hebrew', 'science', 'yearit'], '3': ['yearit', 'english', 'math gila', 'english', 'yizhak', 'yizhak', '', ''], '4': ['english', 'math gila', 'science', 'math rotem', 'sports', 'literature', 'science', 'science'], '5': ['life skills', 'yizhak', 'french', 'yizhak', 'math gila', 'italian', 'italian', 'math gila'], '6': ['yizhak', 'english', 'english', 'math gila', 'science', '', '', ''], '7': ['', '', '', '', '', '', '', '']}

Share an example of oneline style

import json
from pathlib import Path

schedule = json.loads(Path('schedule.json').read_text())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM