VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • Python比较2个字典有哪些值不一致

def get_arbitrage_contract_detail_num():
    sql = """select A,count(A) from tablename group by A """
    data = query_db(sql)
    key = [row[0] for row in data]
    value = [row[1] for row in data]
    arbitrage_contract_detail=dict(zip(key, value))
#如上三句可以优化成下面一句
    arbitrage_contract_detail ={row[0]:row[1] for row in data}
    # print(json.dumps(arbitrage_contract_detail,indent=4,ensure_ascii=False))
    return arbitrage_contract_detail


def txt_dict(file_name):
    with open(file_name, 'r', encoding='utf -8') as f:
        s = []
        for row in f:
            p = r'load contractDetails ([\w\-\.]+),(\d)'
            tmp = re.findall(p, row)
            s.append(tmp)
        key = [x[0][0] for x in s]
        value = [x[0][1] for x in s]
        dict_result = dict(zip(key, value))

#以上三句可以合并为下面一句:
        dict_result = {x[0][0]: x[0][1] for x in s}
        return dict_result


def compare(dict_file, dict_db):
    for k, v in dict_file.items():
        if int(v) != dict_db[k]:
            print(k, '不相等','文件中为',v, '数据库为',dict_db[k])


def main():
    file_name = r'D:\123\123.txt'
    dict_file = txt_dict(file_name)
    dict_db = get_arbitrage_contract_detail_num()
    compare(dict_file, dict_db)


if __name__ == '__main__':
    main()
 
出处:https://www.cnblogs.com/xiaowandian/p/13182877.html


相关教程