python做本地数据与数据库的校验

# coding=utf-8
from pymongo import MongoClient
#连接mongo数据库
env = {
    'dev': {
        'mongodb_host': 'mongodb://账号:密码@服务地址:端口/数据库',
        'mongodb_db': 'titan1',
    },
    'online': {
        'mongodb_host': 'mongodb://账号:密码@服务名:端口/数据库',
        'mongodb_db': 'titan',
    }
}
current_env = 'dev'
client = MongoClient(env[current_env]['mongodb_host'])
database = client[env[current_env]['mongodb_db']]
my_collection=database['car']#连接车辆集合
#需要迁移的店铺
def moveback_shop():
    file = open('/Users/huzhihui/Desktop/change_result.txt','a')
    with open('/Users/huzhihui/Desktop/moveback_shop.txt','r') as f:
        lines = f.readlines()#读取回迁的店铺数据[回迁店铺code 新site_id 新sites 裂变后的大风车店铺B]
    n=0
    for line in lines:
        n+=1
        print  '执行第'+str(n)+''
        file.write('执行第'+str(n)+'')
        # 拿到回迁店铺code
        shop_code = line.strip('
').split()[0]
        b_shop_code = line.strip('
').split()[3]
        # 筛选出工具化二手车和工具化新车集合
        data1 = my_collection.find({'store':"%s"%shop_code})
        #大风车商家发的二手车和新车已经变成新的code,老的code查不到
        if data1.count()!= 0 :
            # 拿到数据库变更后单个车辆的site_id和sites
            x=0
            for single_data in data1:
                x+=1
                print '执行第'+str(x)+'个车辆
'
                file.write('执行第'+str(x)+'个车辆
')
                # 获取site_id
                changed_site_id = single_data.get('site_id')
                # 获取site数组
                changed_sites = single_data.get('sites')
                #获取source
                source=single_data.get('source')
                if source == 'stock_item':
                    #判断是不是集采新车
                    # 拿到文件中基础服务提供的新的site_id
                    new_site_id=line.strip('
').split()[1]
                    # 拿到文件中基础服务提供的新的sites数组
                    new_sites=line.strip('
').split()[2].split(',')
                    new_sites=map(int,new_sites)
                    #对比数据库中新的site_id和店铺服务提供的新的site_id是否相等
                    if changed_site_id == new_site_id:
                        print shop_code+'车辆id:'+str(single_data['_id'])+':site_id成功变更为:'+new_site_id
                        file.write(shop_code+'车辆id:'+str(single_data['_id'])+':site_id成功变更为:'+new_site_id+'
')
                    else:
                        print shop_code+'车辆id:'+str(single_data['_id'])+':site_id变更失败'
                        file.write(shop_code+'车辆id:'+str(single_data['_id'])+':site_id变更失败'+'
')
                    if changed_sites == new_sites:
                        print shop_code+'车辆id:'+str(single_data['_id'])+ ':sites成功变更'
                        file.write(shop_code+'车辆id:' +str(single_data['_id'])+ ':sites成功变更' + '
')
                    else:
                        print shop_code+'车辆id:' +str(single_data['_id'])+ ':sites变更失败'
                        file.write(shop_code+'车辆id:'+str(single_data['_id']) + ':sites变更失败' + '
')
                else:
                    print shop_code+'车辆id:'+str(single_data['_id'])+'变更失败,应该变更店铺code为:'+b_shop_code+'
'
                    file.write(shop_code+'车辆id:'+str(single_data['_id'])+'变更失败,应该变更店铺code为:'+b_shop_code+'
')
        else:
            print '查询失败,无法查询到集采新车'+'
'
            file.write('查询失败,无法查询到集采新车'+'
')
        t=0
        data2 = my_collection.find({'store': "%s" % b_shop_code})
        if data2.count()!=0:
            for car_data in data2:
                t+=1
                print '执行第' + str(t) + '个车辆
'
                file.write('执行第' + str(t) + '个车辆
')
                source = car_data.get('source')
                if  source =='stock_item':
                    print shop_code +'车辆id:'+str(car_data['_id'])+'变更失败,不应该变更店铺code为:'+b_shop_code+'
'
                    file.write(shop_code +'车辆id:'+str(car_data['_id'])+'变更失败,不应该变更店铺code为:'+b_shop_code+'
')
                else:
                    print shop_code +'车辆id:'+str(car_data['_id'])+ '成功变更为:' + b_shop_code + '
'
                    file.write(shop_code +'车辆id:'+str(car_data['_id'])+ '成功变更为:' + b_shop_code + '
')
        else:
            print '查询失败,新老code无法查询到任何车辆'
    file.close()
    f.close()
if __name__=='__main__': moveback_shop() #!/usr/bin/env python # -*- coding: utf-8 -*- import pymysql from pymongo import MongoClient import psycopg2 def sql1(): db = pymysql.connect(host="", user="", password="", db="", port=3306, charset='utf8') #已二进制格式打开文件,用于读取 with open("/Users/huzhihui/Desktop/shop.txt", "rb") as f: lines = f.readlines() #记录错误数据 rf = open("/Users/huzhihui/Desktop/错误数据1", "wb") #记录错误次数 ere = 0 #记录比对的数据数量 n = 0 for line in lines: line = line.strip(' ') cur = db.cursor() print line # print isinstance(line,str) try: cur.execute('select * from baseshop WHERE code="%s"'%line) # cur.execute('select * from baseshop WHERE code=" 13088303331"') # print 'select * from baseshop WHERE code="%s"'%line # 获取所有记录列表 results = cur.fetchall() assert results[0][0]==1954 print results[0][0] print results[0][1] print results[0][2] except Exception,e: ere += 1 rf.writelines(str(e)+ "------" + line) finally: n += 1 print "run:" + "" + str(n) + "错误" + str(ere) def psycopg1(): conn = psycopg2.connect(database="cheniu_trade_dev1", user="dev1_trade", password="dev1_trade_pwd", host="", port="5432") cur = conn.cursor() cur.execute('select * from cheniu_orders where id = 113395') rows = cur.fetchall() for row in rows: print "ID = ", row[0] print "NAME = ", row[1] print "ADDRESS = ", row[2] print "SALARY = ", row[3], " " def readFromFile(): f = open("/Users/zwq/Desktop/shop_old.txt", "rb") lines = f.readlines() for line in lines: shopcode, oldcode = line.strip(' ').split(' ') print "shopcode:" + shopcode + " oldcode:" + oldcode f.close() def writeToFile(): rf = open("/Users/zwq/Desktop/shop_add.txt", "w+") f = open("/Users/zwq/Desktop/shop.txt", "r+") #从shop.txt读数据,生成两条数据并写入shop_add.txt lines = f.readlines() for line in lines: line = line.strip(' ') rf.write(line + ' ' + line + "old" + " ") #去除最后一行的换行 rf.seek(-1, 2) if rf.read(1) == " ": rf.seek(-1, 2) rf.truncate() rf.close() f.close() def readFromFile1(): f = open("/Users/zwq/Desktop/shop_old.txt", "rb") n = 0 f.seek(-1,2) while True: n += 1 print str(n) + "-------" lines = f.read(1) print lines if n ==30: break if lines == " ": print "oooooooooooooo" def writeToFile1(): rf = open("/Users/zwq/Desktop/shop_old.txt", "r+") f = open("/Users/zwq/Desktop/shop.txt", "r+") lines = f.readlines() for line in lines: line = line.strip(' ') rf.write(line + ' ' + line + "old" + " ") rf.seek(-1, 2) if rf.read(1) == " ": rf.seek(-1, 2) rf.truncate() rf.close() def mongo2(): conn = MongoClient('115.29.204.169', 27017) db = conn.gaea # 连接mydb数据库,没有则自动创建 db.authenticate("gaea", "gaea") my_collection = db.user #连接user集合 data = my_collection.find({"iid":1111049}) for item in data: print item #print data if __name__ == '__main__': # sql1() # readFromFile()#writeToFile() # writeToFile1() # readFromFile1() # psycopg1() #print ret
原文地址:https://www.cnblogs.com/hzh1028/p/9444096.html