我想用 Python 讀取一個 JSON 文件:這是我的 JSON 文件的一部分:{ "Jointure":[ { "IDJointure":1, "societe":"S.R.T.K", "date":"2019/01/01", "heure":"05:47:00"}, { "IDJointure":2, "societe":"S.R.T.K", "date":"2019/01/01", "heure":"05:50:00"}]}這是代碼:import jsondata = json.loads('Data2019.json')for i in data['Jointure']: print(i) 但是,這是顯示的錯誤Traceback (most recent call last): File "C:\Users\HP\Desktop\readJSON.py", line 4, in <module> data = json.loads('Data2019.json') File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads return _default_decoder.decode(s) File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from Nonejson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)>>>
3 回答

人到中年有點甜
TA貢獻(xiàn)1895條經(jīng)驗 獲得超7個贊
json.loads()
期望 json 數(shù)據(jù)已經(jīng)是一個字符串——所以它試圖將文件名 Data2019.json
解釋為實際的 json 數(shù)據(jù)。
打開文件,然后將文件對象傳遞給json.load()
:
with open('Data2019.json') as fp: data = json.load(fp)

富國滬深
TA貢獻(xiàn)1790條經(jīng)驗 獲得超9個贊
不要直接讀取文件。打開文件,它只是與 json 模塊一起使用的文件內(nèi)容。嘗試這個:
import json with open('path_to_file/person.json') as f: data = json.load(f)

鳳凰求蠱
TA貢獻(xiàn)1825條經(jīng)驗 獲得超4個贊
試試熊貓
import pandas as pd
patients_df = pd.read_json('E:/datasets/patients.json')
patients_df.head()
添加回答
舉報
0/150
提交
取消