python2排序
按時間 排序 :http://blog.csdn.net/stan_pcf/article/details/51969878
關鍵詞重要性排序 :
keydict = sorted(詞典名.iteritems(), key=lambda d: d[1], reverse=True)
#d[0]:按key排序 d[1]:按value排序
加載json文件 # json文件一定是utf8無BOM格式否則報錯
def load_datas():
basePath = path.abspath(path.dirname(__file__)) #前兩句在falsk主文件中必須使用
uploadPath = path.join(basePath, 'files/history.json')
f = file(uploadPath)
jsonDatas = json.load(f)
加載文件多使用with
https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/
with open(r'somefileName') as somefile:
for line in somefile:
print line
# ...more code
python2的編碼格式轉換
http://python.jobbole.com/87145/
python3 json按屬性字段排序
json_array = [{"time":20150312,"value":"c"}, {"time":20150301,"value":"a"}, {"time":20150305,"value":"b"}]
json_array.sort(key = lambda x:x["time"])#可以加參數reverse=True倒序
print(json_array)
結果:
[{'value': 'a', 'time': 20150301},
{'value': 'b', 'time': 20150305},
{'value': 'c', 'time': 20150312}]
將str類型的字段數據轉換爲json格式:
json.loads(data)