當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。