尝试加载json.Python时出错

2024-09-28 16:43:54 发布

您现在位置: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)

如何解决此错误?谢谢你的帮助


2条回答

正如@buran所指出的,文件末尾有额外的逗号,具体如下:

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

但是仍然可以使用^{} method处理该文件:

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

分享一个单线风格的例子

import json
from pathlib import Path

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

相关问题 更多 >