繁体   English   中英

尝试 json.load Python 时出错

[英]Getting an error trying to json.load Python

我正在尝试制作一些程序来告诉我在特定的时间和日期我上了什么课。 为了让它工作,我把我的日程安排在一个 json 文件中(数字对应于天数)。

{
    "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": [
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "", 
    ],
}

我使用此代码将其转换为 python 字典:

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

但它给了我这个错误信息:

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)

我该如何解决这个错误? 任何帮助表示赞赏。

正如@buran 指出的那样,您在文件末尾有额外的逗号,特别是在这里:

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

但是仍然可以使用ast.literal_eval方法处理该文件:

import ast

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

印刷:

{'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': ['', '', '', '', '', '', '', '']}

分享一个oneline样式的例子

import json
from pathlib import Path

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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