• python 把数据 json格式输出


    有个要求需要在python的标准输出时候显示json格式数据,如果缩进显示查看数据效果会很好,这里使用json的包会有很多操作

    import json
    
    date = {u'versions': [{u'status': u'CURRENT', u'id': u'v2.3', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.2', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.1', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.0', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v1.1', u'links': [{u'href': u'http://controller:9292/v1/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v1.0', u'links': [{u'href': u'http://controller:9292/v1/', u'rel': u'self'}]}]}
    
    print json.dumps(data, sort_keys=True, indent=2) # 排序并且缩进两个字符输出
    

     

    这样就会得到如下的输出:

    {
      "versions": [
        {
          "id": "v2.3", 
          "links": [
            {
              "href": "http://controller:9292/v2/", 
              "rel": "self"
            }
          ], 
          "status": "CURRENT"
        }, 
        {
          "id": "v2.2", 
          "links": [
            {
              "href": "http://controller:9292/v2/", 
              "rel": "self"
            }
          ], 
          "status": "SUPPORTED"
        }, 
        {
          "id": "v2.1", 
          "links": [
            {
              "href": "http://controller:9292/v2/", 
              "rel": "self"
            }
          ], 
          "status": "SUPPORTED"
        }, 
        {
          "id": "v2.0", 
          "links": [
            {
              "href": "http://controller:9292/v2/", 
              "rel": "self"
            }
          ], 
          "status": "SUPPORTED"
        }, 
        {
          "id": "v1.1", 
          "links": [
            {
              "href": "http://controller:9292/v1/", 
              "rel": "self"
            }
          ], 
          "status": "SUPPORTED"
        }, 
        {
          "id": "v1.0", 
          "links": [
            {
              "href": "http://controller:9292/v1/", 
              "rel": "self"
            }
          ], 
          "status": "SUPPORTED"
        }
      ]
    }
    

     可以看到都已经格式化了。

    这是在python中,如果直接使用命令行,希望直接转换,可以使用 data | python -mjson.tool 来输出json格式的数据

    echo '{"first_key": "value", "second_key": "value2"}' | python -mjson.tool 
    

    比如想直接在命令行中过滤得到first_key对于的值,那么这样即可:

    echo '{"first_key": "value", "second_key": "value2"}' | python -c 'import sys, json; print json.load(sys.stdin)[sys.argv[1]]' first_key
    

    就会得到对于的value了。

    参考资料:

    http://liuzhijun.iteye.com/blog/1859857

    http://docs.python.org/2/library/json.html

    http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html
    http://pymotw.com/2/json/

  • 相关阅读:
    Kaggle 神器 xgboost
    改善代码可测性的若干技巧
    IDEA 代码生成插件 CodeMaker
    Elasticsearch 使用中文分词
    Java性能调优的11个实用技巧
    Lucene 快速入门
    Java中一个字符用unicode编码为什么不是两字节
    lucene 的评分机制
    面向对象设计的 10 条戒律
    2019.10.23-最长全1串(双指针)
  • 原文地址:https://www.cnblogs.com/juandx/p/4953397.html
Copyright © 2020-2023  润新知