python+django读取json数据,同步数据库

[["account_detail", 1],
["account", 1], ["account_recharge", 3]]

########################################################

#!/usr/bin/python
#coding=utf-8
from django.core.management import setup_environ
#import sys
#sys.path.append(‘..‘)
from payment import settings
setup_environ(settings)
#------------------------------------
from datetime import datetime,date,timedelta
from payment.models import *
from django.db.models import Q,Avg,Sum,Count
from django.db import connection
from django.db import transaction
from django.contrib.sessions.models import Session
import json
import time
from decimal import *
from payhandler import mytest,plans
from payment.app import log
import os
import sys
import re

logger=log.get_logger(‘syncdb‘,__name__)
data={}


def writetxt(jdata):
    f=open(‘../../../payment_shadow/syncdb.txt‘,‘w‘)
    f.write(jdata)
    f.close()
def test4(data1):
    #把所有表对象放到一个元组中
    tab=(account,account_detail,account_recharge,account_recharge_log,user_login_log,credit_class,credit_rule,
         festival_datelist,excute_datelist,schedule_list,credit_exec,partner_credit,
         partner_credit_detail,latefee_detail,account_status_log,messages,ignore_date_list,amount_level,
         account_daily_summary,account_daily_summary_detail)#需要同步的对象
    for key ,values in data1.items():
        tabstr=values[0]
        if(len(values)==1)
            id=1
        id=int(values[2])
        for j in tab:
            strcut= str(j)[23:-2]
            if cmp(strcut,tabstr)==0:
                    print strcut
                    r =j.objects.using(‘aa‘).order_by(‘id‘).filter(pk__gt=id)[:2]
                    if len(r)>0:
                        for rvalue in r:
                            rvalue.save()
                            value1=rvalue
                        print (u‘数据库:%s,同步到id为:%s‘%(tabstr,value1.id))
                        data2=[]
                        data2.append(tabstr)
                        data2.append(value1.id)
                        data[tabstr]=data2
                        #data.append(data2)

                    else:
                         print(u"未发现没有同步的数据")




#读取txt中需要同步的json数据
def readjson():
    f =open("../../../payment_shadow/log.txt",‘r‘)
    line = f.readline()
    fdata=line
    while line:
        line=f.readline()
        fdata=fdata+line

    f.close()

    print (u"***********************同步数据库开始***************************")
    starttime=time.time()
    print fdata
    data1 = json.loads(fdata)
    test4(data1)
    print data1[0]
    jsondata=json.dumps(data)
    print jsondata
    writetxt(jsondata)
    endtime=time.time()
    print("同步数据所需的时间:"+str(endtime-starttime)+"秒")
    print (u"***********************同步数据库结束***************************")




if __name__==‘__main__‘:
    readjson()



python+django读取json数据,同步数据库,布布扣,bubuko.com

python+django读取json数据,同步数据库

上一篇:mac中apache+mysql+php+phpMyAdmin配置备忘


下一篇:C#开发PACS医学影像处理系统(九):序列控件与拖拽