zoukankan      html  css  js  c++  java
  • 高效的几个小技巧

    一、json

    json 序列化dumps之后,数据会变成很长的一行,如果,数据量非常大就会相当不易查看,使用indent参数来输出便于查看的JSON,,应该是一个非负的整型,默认是None,如果设置indent=0,则跟None一样,数据还是一行。

    使用sort_key将数据根据keys的值进行排序,sort_key默认为False

    如:

    { "终点站": "贵阳站", "车号": "K607", "日期": "2016年01月2日", "金额": "278.5", "座位号": "18车063号", "座位类型": "新空调硬座"}

    且序列化之后默认为ascii格式,不便于查看,我们禁用ascii编码转化为utf-8编码。

    小技巧如下:

    jsonstr =  json.dumps(str,indent=2,ensure_ascii=False,sort_keys=True)

    jsonstr.encode(utf-8)

    {
    "终点站": "贵阳站",
    "车号": "K607",
    "日期": "2016年01月2日",
    "金额": "278.5",
    "座位号": "18车063号",
    "座位类型": "新空调硬座"
    }

    写入文件时也可以在打开文件时注明编码格式:

    jsonstr =  json.dumps(str,indent=2,ensure_ascii=False)

    with open('jsonstr.txt','a',encoding='utf-8')  as f:

      f.write(jsonstr)

    二、循环

    numbers =  [11,12,34,56]

    fro i  in enumerate()

    
    
  • 相关阅读:
    BSGS算法(大步小步算法)
    UVA-11426【GCD
    UVA-1637【Double Patience】(概率dp)
    UVA-11174【Stand in a Line】
    About
    51nod 1355 斐波那契的最小公倍数
    CodeForces
    CodeForces
    CodeForces
    CodeForces 901C Bipartite Segments
  • 原文地址:https://www.cnblogs.com/wzjbg/p/7422247.html
Copyright © 2011-2022 走看看