• 读取json数据转换为字典存入到列表当中【多测师_王sir】


    data.json数据如下:
    {
          "WeChatVIPV8_LOGIN:微信星光会员账号":"123",
          "WeChatVIP_LOGIN:微信会员账号":"123",
          "WeChatNOTVIP_LOGIN:微信非会员账号":"123",
          "QQNOTVIP_SpecialZone_LOGIN:QQ特区账号":"123"
    }
    #coding=utf-8
    """
    ===========================
    Author:多测师_王sir
    Time:2020-09-08 22:20
    Wechat:xiaoshubass
    website:www.duoceshi.cn
    ===========================
    """
    
    
    
    import json
    import re
    
    new_dict = {}
    def to_dict(dict1):
        '''封装一个转换为字典的函数'''
        for k,v in dict1.items():
            if isinstance(v,dict):
                to_dict(v)
            elif isinstance(v,list):
                for j in v:
                    to_dict(j)
            else:
                new_dict[k] = v
        return new_dict
    
    def read_json(path):
        wechat_list=[]
        qq_list=[]
        wechat_dict={}
        qq_dict={}
        o = open(path,'r',encoding='utf-8')
        all = o.read()
        dict1 = json.loads(all)
        value = to_dict(dict1)
        wechat_reg = re.compile('WeChat+')
        qq_reg = re.compile('QQ+')
        for k,v in value.items():
            if wechat_reg.findall(k):
                wechat_dict.setdefault(k, v)
            elif qq_reg.findall(k):
                qq_dict.setdefault(k, v)
        wechat_list.append(wechat_dict)
        qq_list.append(qq_dict)
        print(wechat_list)
        print(qq_list)
    
    if __name__ == '__main__':
        read_json('C:projectdcs6decoratordata.json')
    读取出来的结果如下:
    [{'WeChatVIPV8_LOGIN:微信星光会员账号': '123', 'WeChatVIP_LOGIN:微信会员账号': '123', 
    'WeChatNOTVIP_LOGIN:微信非会员账号': '123'}]
    [{
    'QQNOTVIP_SpecialZone_LOGIN:QQ特区账号': '123'}]
  • 相关阅读:
    Sectong日志分析
    风暴中心
    安全趋势:
    Mysql table ful
    linux service命令常见使用方法
    client denied by server configuration
    Ubuntu配置apache
    O2O助汪峰成功逆袭,汪峰最终上头条了
    Android切图注意事项
    2014/08/23——OJ及相关站点打开速度非常慢,训练计划login直接error!
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/13636067.html
Copyright © 2020-2023  润新知