Python + django reads json data and synchronizes the database

Source: Internet
Author: User

[["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 ):
# Put all table objects in a single tuples
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) # object to be synchronized
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 ('A'). order_by ('id'). filter (pk _ gt = id) [: 2]
If len (r)> 0:
For rvalue in r:
Rvalue. save ()
Value1 = rvalue
Print (u'database: % s, synchronized to id: % s' % (tabstr, value1.id ))
Data2 = []
Data2.append (tabstr)
Data2.append (value1.id)
Data [tabstr] = data2
# Data. append (data2)

Else:
Print (u "no synchronized data found ")




# Reading json data to be synchronized in txt
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 ("time required for data synchronization:" + str (endtime-starttime) + "second ")
Print (u ********** *****************")




If _ name __= = '_ main __':
Readjson ()



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.