def load(abspath, default=None, enable_verbose=True):
"""Load Json from file. If file are not exists, returns ``default``.
:param abspath: file path. use absolute path as much as you can.
extension has to be ``.json`` or ``.gz`` (for compressed Json).
:type abspath: string
:param default: default ``dict()``, if ``abspath`` not exists, return the
default Python object instead.
:param enable_verbose: default ``True``, help-message-display trigger.
:type enable_verbose: boolean
Usage::
>>> from dataIO import js
>>> js.load("test.json") # if you have a json file
Load from 'test.json' ...
Complete! Elapse 0.000432 sec.
{'a': 1, 'b': 2}
**????**
?Json???????
:param abspath: Json??????, ????? ``.json`` ? ``.gz``, ?? ``.gz``
??????Json??
:type abspath: ``???``
:param default: ?? ``dict()``, ?????????, ??????????
:param enable_verbose: ?? ``True``, ???????, ????????
:type enable_verbose: ``???``
"""
if default is None:
default = dict()
prt("\nLoad from '%s' ..." % abspath, enable_verbose)
abspath = lower_ext(str(abspath))
is_json = is_json_file(abspath)
if not os.path.exists(abspath):
prt(" File not found, use default value: %r" %
default, enable_verbose)
return default
st = time.clock()
if is_json:
data = json.loads(textfile.read(abspath, encoding="utf-8"))
else:
data = json.loads(compress.read_gzip(abspath).decode("utf-8"))
prt(" Complete! Elapse %.6f sec." % (time.clock() - st), enable_verbose)
return data
评论列表
文章目录