簡體   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