Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實例
前言
最近在python里面用json讀取json文件,可是老是不成功,特此記錄一下。
預備知識:
def load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document) to a Python object.""" def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): """Deserialize ``s`` (a ``str`` instance containing a JSON document) to a Python object."""
其實我剛剛看json.load()和json.loads()代碼定義的時候,也不知道什么是文件類型。什么是字符串類型,用python的type函數(shù)看一下就好了
例如:
with open("文件名") as f: print(type(f)) # <class '_io.TextIOWrapper'> 也就是文本IO類型 result=json.load(f) with open("文件名") as f: line=f.readline(): print(type(line)) # <class 'str'> result=json.loads(line)
使用方法
從以上可以看出json.load()是用來讀取文件的,即,將文件打開然后就可以直接讀取。示例如下:
with open("文件名") as f: result=json.load(f)
json.loads()是用來讀取字符串的,即,可以把文件打開,用readline()讀取一行,然后json.loads()一行。示例如下:
#json文件為: {"outputs": ["pool1/7x7/ets", "pool1/7x7/rf", "pool1/10x10/ets", "pool1/10x10/rf", "pool1/13x13/ets", "pool1/13x13/rf"]}
讀取代碼如下:
with open("文件名") as f: line=f.readline(): result=json.loads(line)
當json文件如下時,讀取內(nèi)容是錯誤的:
{ "dataset":{ "train": {"type": "mnist", "data_set": "train", "layout_x": "tensor"}, "test": {"type": "mnist", "data_set": "test", "layout_x": "tensor"} }, "train":{ "keep_model_in_mem":0, "random_state":0, "data_cache":{ "cache_in_disk":{ "default":1 }, "keep_in_mem":{ "default":0 }, "cache_dir":"/mnt/raid/fengji/gcforest/mnist/fg-tree500-depth100-3folds/datas" } }, "net":{ "outputs": ["pool1/7x7/ets", "pool1/7x7/rf", "pool1/10x10/ets", "pool1/10x10/rf", "pool1/13x13/ets", "pool1/13x13/rf"], "layers":[ { "type":"FGWinLayer", "name":"win1/7x7", "bottoms": ["X","y"], "tops":["win1/7x7/ets", "win1/7x7/rf"], "n_classes": 124, "estimators": [ {"n_folds":3,"type":"ExtraTreesClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10}, {"n_folds":3,"type":"RandomForestClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10} ], "stride_x": 2, "stride_y": 2, "win_x":7, "win_y":7 }, { "type":"FGWinLayer", "name":"win1/10x10", "bottoms": ["X","y"], "tops":["win1/10x10/ets", "win1/10x10/rf"], "n_classes": 10, "estimators": [ {"n_folds":3,"type":"ExtraTreesClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10}, {"n_folds":3,"type":"RandomForestClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10} ], "stride_x": 2, "stride_y": 2, "win_x":10, "win_y":10 }, { "type":"FGWinLayer", "name":"win1/13x13", "bottoms": ["X","y"], "tops":["win1/13x13/ets", "win1/13x13/rf"], "n_classes": 10, "estimators": [ {"n_folds":3,"type":"ExtraTreesClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10}, {"n_folds":3,"type":"RandomForestClassifier","n_estimators":500,"max_depth":100,"n_jobs":-1,"min_samples_leaf":10} ], "stride_x": 2, "stride_y": 2, "win_x":13, "win_y":13 }, { "type":"FGPoolLayer", "name":"pool1", "bottoms": ["win1/7x7/ets", "win1/7x7/rf", "win1/10x10/ets", "win1/10x10/rf", "win1/13x13/ets", "win1/13x13/rf"], "tops": ["pool1/7x7/ets", "pool1/7x7/rf", "pool1/10x10/ets", "pool1/10x10/rf", "pool1/13x13/ets", "pool1/13x13/rf"], "pool_method": "avg", "win_x":2, "win_y":2 } ] } }
因為在代碼中,json.loads()并沒有讀取完整的json文件,只是讀取了行,所以這時json.loads(line)讀取的是不合符json語法的字符串,會報錯:
with open("文件名") as f: line=f.readline(): # 這里line只是讀取了json文件的一行,并沒有全部讀取,所以line里面所存的字符串是不符合json語法的,所以讀取出錯。 result=json.loads(line) Traceback (most recent call last): File "D:/PycharmProjects/mnistCheck/test.py", line 12, in <module> result = json.loads(row) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\json\__init__.py", line 319, in loads return _default_decoder.decode(s) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\json\decoder.py", line 355, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
那么問題來了。。。。在實際應用中,我們會在json文件中做注釋,比如以“//”開頭的注釋,除了注釋部分外,其他內(nèi)容都是符合json語法的,那么我們要怎么處理呢?
def load_json(path): import json lines = [] # 第一步:定義一個列表, 打開文件 with open(path) as f: for row in f.readlines(): # 第二步:讀取文件內(nèi)容 if row.strip().startswith("http://"): # 第三步:對每一行進行過濾 continue lines.append(row) # 第四步:將過濾后的行添加到列表中. return json.loads("\n".join(lines)) #將列表中的每個字符串用某一個符號拼接為一整個字符串,用json.loads()函數(shù)加載,這樣就大功告成啦??!
總結(jié)
到此這篇關于Pythonh中使用json.load()和json.loads()加載json數(shù)據(jù)的文章就介紹到這了,更多相關Pythonh加載json數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python中JSON常見用法(json.load()、json.loads()、json.dump()、json.dumps())
- python中json.dumps()和json.loads()的用法
- Python中json.load()和json.loads()有哪些區(qū)別
- 解決Python下json.loads()中文字符出錯的問題
- Python中json.dumps()函數(shù)的使用解析
- python json.dumps() json.dump()的區(qū)別詳解
- python字典和json.dumps()的遇到的坑分析
- Python的json.loads() 方法與json.dumps()方法及使用小結(jié)
相關文章
np.concatenate()函數(shù)數(shù)組序列參數(shù)的實現(xiàn)
本文主要介紹了np.concatenate()函數(shù)數(shù)組序列參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03解讀pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)
這篇文章主要介紹了pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09ubuntu20.04運用startup application開機自啟動python程序的腳本寫法
這篇文章主要介紹了ubuntu20.04運用startup application開機自啟動python程序的腳本寫法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10