本文整理匯總了Python中json.tool方法的典型用法代碼示例。如果您正苦於以下問題:Python json.tool方法的具體用法?Python json.tool怎麽用?Python json.tool使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類json
的用法示例。
在下文中一共展示了json.tool方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: json_tool
# 需要導入模塊: import json [as 別名]
# 或者: from json import tool [as 別名]
def json_tool(filepath, check=False) -> bool:
"""
Equivalent to json.tool utility, except returns whether changes were made
Returns whether changes were made
"""
with open(filepath) as f:
content = f.read()
obj = json.loads(content)
output = json_dumps_pretty(obj)
if output != content:
if not check:
with open(filepath, "w") as f:
f.write(output)
return True
return False
示例2: main
# 需要導入模塊: import json [as 別名]
# 或者: from json import tool [as 別名]
def main():
prog = 'python -m json.tool'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description)
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
help='a JSON file to be validated or pretty-printed')
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
help='write the output of infile to outfile')
parser.add_argument('--sort-keys', action='store_true', default=False,
help='sort the output of dictionaries alphabetically by key')
options = parser.parse_args()
infile = options.infile or sys.stdin
outfile = options.outfile or sys.stdout
sort_keys = options.sort_keys
with infile:
try:
obj = json.load(infile)
except ValueError as e:
raise SystemExit(e)
with outfile:
json.dump(obj, outfile, sort_keys=sort_keys, indent=4)
outfile.write('\n')
示例3: get_metadata_uncached
# 需要導入模塊: import json [as 別名]
# 或者: from json import tool [as 別名]
def get_metadata_uncached():
"""Returns the GCE metadata as a dict.
Returns None if not running on GCE or if metadata server is unreachable.
Refs:
https://cloud.google.com/compute/docs/metadata
https://cloud.google.com/compute/docs/machine-types
To get the at the command line from a GCE VM, use:
curl --silent \
http://metadata.google.internal/computeMetadata/v1/?recursive=true \
-H "Metadata-Flavor: Google" | python -m json.tool | less
"""
raw = _raw_metadata_request('/computeMetadata/v1/?recursive=true')
return json.loads(raw) if raw else None
示例4: main
# 需要導入模塊: import json [as 別名]
# 或者: from json import tool [as 別名]
def main():
prog = 'python -m json.tool'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description)
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
help='a JSON file to be validated or pretty-printed')
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
help='write the output of infile to outfile')
parser.add_argument('--sort-keys', action='store_true', default=False,
help='sort the output of dictionaries alphabetically by key')
options = parser.parse_args()
infile = options.infile or sys.stdin
outfile = options.outfile or sys.stdout
sort_keys = options.sort_keys
with infile:
try:
if sort_keys:
obj = json.load(infile)
else:
obj = json.load(infile,
object_pairs_hook=collections.OrderedDict)
except ValueError as e:
raise SystemExit(e)
with outfile:
json.dump(obj, outfile, sort_keys=sort_keys, indent=4)
outfile.write('\n')