当前位置: 首页>>代码示例>>Python>>正文


Python json.tool方法代码示例

本文整理汇总了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 
开发者ID:twosixlabs,项目名称:armory,代码行数:21,代码来源:format_json.py

示例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') 
开发者ID:CedricGuillemet,项目名称:Imogen,代码行数:26,代码来源:tool.py

示例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 
开发者ID:luci,项目名称:luci-py,代码行数:18,代码来源:gce.py

示例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') 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:30,代码来源:tool.py


注:本文中的json.tool方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。