Pythonh中⽤json.load()json.loads()加载json数据的⽅法
最近在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函数看⼀下就好了
例如:linux系统是一个
with open("⽂件名") as f:
print(type(f)) # <class '_io.TextIOWrapper'> 也就是⽂本IO类型
result=json.load(f)
with open("⽂件名") as f:
adline():
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:
adline():
result=json.loads(line)
当json⽂件如下时,读取内容是错误的:
{
"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":{
"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"],
python请求并解析json数据"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,
ps怎么把图片切下来"win_y":10
},
{
"type":"FGWinLayer",
"name":"win1/13x13",
"bottoms": ["X","y"],
"tops":["win1/13x13/ets", "win1/13x13/rf"],
"n_classes": 10,
"estimators": [
excel连接函数{"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:
adline(): # 这⾥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
tool在线工具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)java单元测试用例怎么写
那么问题来了。。。。在实际应⽤中,我们会在json⽂件中做注释,⽐如以“//”开头的注释,除了注释部分外,其他内容都是符合json语法的,那么我们要怎么处理呢?
def load_json(path):
import json
lines = [] # 第⼀步:定义⼀个列表,打开⽂件
with open(path) as f:
for row adlines(): # 第⼆步:读取⽂件内容
if row.strip().startswith("//"): # 第三步:对每⼀⾏进⾏过滤
continue
lines.append(row) # 第四步:将过滤后的⾏添加到列表中.
return json.loads("\n".join(lines)) #将列表中的每个字符串⽤某⼀个符号拼接为⼀整个字符串,⽤json.loads()函数加载,这样就⼤功告成啦!!今天的学习就到这⾥,明天见!!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论