zoukankan      html  css  js  c++  java
  • python学习笔记(十一)处理json

    json串就是字符串,json串里必须是双引号

     1 d={'car':{'color':'red','price':100,'count':50},
     2     '爱分叉':{'color':'red','price':100,'count':50},
     3     'iphoneX1':{'color':'red','price':100,'count':50},
     4     'iphoneX2':{'color':'red','price':100,'count':50},
     5     'iphoneX3':{'color':'red','price':100,'count':50},
     6     'iphoneX4':{'color':'red','price':100,'count':50}
     7    }
     8 res=json.dumps(d,indent=4,ensure_ascii=False)#把字典,list转成json,indent多少缩进,ensure_ascii可以显示中文
     9 print(res)
    10 f=open('f1','w',encoding='utf-8')
    11 f.write(res)
    12 
    13 
    14 f1=open('f1',encoding='utf-8')
    15 res=f1.read()
    16 print(type(res))#<class 'str'>
    17 dict_res=json.loads(res)#把json串变成python的数据类型
    18 print(type(dict_res))#<class 'dict'>
    19 print(dict_res)
    20 
    21 f2=open('f2','w',encoding='utf-8')
    22 json.dump(d,f2,indent=4,ensure_ascii=False)#自动帮你写入文件,第一个参数是数据,第二个是文件对象
    23 
    24 f2=open('f2',encoding='utf-8')
    25 print(json.load(f2))#自动帮你读入文件
  • 相关阅读:
    集训队作业2018人类的本质
    推式子小技巧
    [Codeforces671D]Roads in Yusland
    线性规划的对偶问题
    数学虐哭空巢老人记
    Voronoi图与Delaunay三角剖分
    [ZJOI2018]保镖
    [SPOJ2939]Qtree5
    数据结构虐哭空巢老人记
    [CTSC2006]歌唱王国
  • 原文地址:https://www.cnblogs.com/wxcx/p/8290539.html
Copyright © 2011-2022 走看看