1. 程式人生 > >python day28--json,pickle,hashlib,logging

python day28--json,pickle,hashlib,logging

一、json格式的限制

1.json格式的key必須是字串資料型別,如果是數字dumps後會被轉為字串。

# dic = {1:2,3:4}
# str_dic = json.dumps(dic)
# print(str_dic)
# new_dic = json.loads(str_dic)
# print(new_dic)

2.元組可以作為vaule但會被轉為列表,並且不能作為key

3.可以多次將資料dump進檔案,但不能load出來。

# dic = {'abc':(1,2,3)}
# lst = ['aaa',123,'bbb',12.456]
# with open('json_demo','w') as f:
# json.dump(lst,f) # json.dump(dic,f) # with open('json_demo') as f: # ret = json.load(f) # print(ret)
#報錯