读取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:\project\dcs6\decorator\data.json')
读取出来的结果如下:
[{'WeChatVIPV8_LOGIN:微信星光会员账号': '123', 'WeChatVIP_LOGIN:微信会员账号': '123', 
'WeChatNOTVIP_LOGIN:微信非会员账号': '123'}]
[{
'QQNOTVIP_SpecialZone_LOGIN:QQ特区账号': '123'}]

 

posted @ 2020-09-08 22:23  多测师_树哥  阅读(218)  评论(0编辑  收藏  举报