python-json?????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-json?????????相关的知识,希望对你有一定的参考价值。
??????????????? with open res print ???????????? highlight ?????? python
??????json??????????????????
1????????????????????????
2??????????????????????????????
json????????????????????????
dumps ,loads??????????????????
dump,load ??????????????????
json ????????????????????????????????? : json?????????????????? ???python??????????????? ??????java????????????????????????
????????????????????????????????????????????? : ????????? ?????? ?????? ??????
????????????key??????????????????
?????????????????????????????????
# ??????1 ??????????????????????????? # dic = {1 : ???value???,2 : ???value2???} # ret = json.dumps(dic) # ????????? # print(dic,type(dic)) # print(ret,type(ret)) # # res = json.loads(ret) # ???????????? # print(res,type(res)) # ??????2 ???????????????????????? # dic = {1 : [1,2,3],2 : (4,5,???aa???)} # ret = json.dumps(dic) # ????????? # print(dic,type(dic)) # print(ret,type(ret)) # res = json.loads(ret) # ???????????? # print(res,type(res)) # ??????3 # s = {1,2,???aaa???} # json.dumps(s) # ??????4 # TypeError: keys must be a string ??????key???????????????????????? # json.dumps({(1,2,3):123})
??????
??????dump?????????load???????????????????????????dump???load
# ??????5 ????????????????????? ??? # dic = {???key1??? : ???value1???,???key2??? : ???value2???} # with open(???json_file???,???a???) as f: # json.dump(dic,f) # json.dump(dic,f) # json.dump(dic,f) # with open(???json_file???,???r???) as f: # dic = json.load(f) # print(dic.keys())
??????
?????????????????????????????????dumps???loads????????????????????????????????????????????????????????????????????????????????????
# ?????? :???????????????????????????????????????????????????,??????????????????????????? # dic = {???key1??? : ???value1???,???key2??? : ???value2???} # # with open(???json_file???,???a???) as f: # str_dic = json.dumps(dic) # f.write(str_dic+??? ???) # str_dic = json.dumps(dic) # f.write(str_dic + ??? ???) # str_dic = json.dumps(dic) # f.write(str_dic + ??? ???) # with open(???json_file???,???r???) as f: # for line in f: # dic = json.loads(line.strip()) # print(dic.keys())
??????
以上是关于python-json?????????的主要内容,如果未能解决你的问题,请参考以下文章