新聞中心
1、常見數(shù)據(jù)庫

目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、榆社網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
(1)Scikit-learn:需要復(fù)蓋特征工程、模型訓(xùn)練和模型測試所有功能的程序庫,Scikit-learn是最好的選擇。這個優(yōu)秀的免費軟件可以提供機器學(xué)習(xí)和數(shù)據(jù)挖掘所需的所有工具,現(xiàn)在是python機器學(xué)習(xí)的標(biāo)準(zhǔn)庫,建議使用成熟的機器學(xué)習(xí)算法。
(2)NLTK:雖然不是機器學(xué)習(xí)的程序庫,但它是自然語言處理所必需的庫。除了文本處理功能之外,它還包括聚類、分詞、詞干提取、標(biāo)記、分析等大量數(shù)據(jù)集和其他關(guān)于詞法的資源。
2、轉(zhuǎn)移數(shù)據(jù)
基于Python2.7的版本環(huán)境,Python實現(xiàn)了數(shù)據(jù)庫的跨服務(wù)器遷移,每次提交查詢都要5000條,代碼中每個查詢提交的數(shù)量可以自己改變。
# -*- coding: utf-8 -*-
import MySQLdb
import time
import warnings
warnings.filterwarnings("ignore")
class ConnectMysql(object):
def __init__(self):
# 這里設(shè)置分頁查詢, 每頁查詢多少數(shù)據(jù)
self.page_size = 5000
def getTable(self):
conn = MySQLdb.connect(
host="***.***.**.**",
user="****",
passwd="*************",
db='****',
charset='utf8'
)
conn_local = MySQLdb.connect(
host="********************************",
user="**********",
passwd="********",
db='*******',
charset='utf8'
)
cur = conn.cursor()
cur_local = conn_local.cursor()
cur.execute('show tables')
tables = cur.fetchall()
for table in tables:
print str(table[0]).lower()
# 需要遷移的數(shù)據(jù)庫查詢表的列數(shù)
cur.execute("SELECT COUNT(*) FROM information_schema.COLUMNS WHERE table_schema='china' AND table_name='" + table[0] + "'")
table_col_count = cur.fetchone()
# print table_col_count[0]
# 需要遷移的數(shù)據(jù)庫查詢表的結(jié)構(gòu)
cur.execute('show create table ' + table[0])
result = cur.fetchall()
create_sql = result[0][1]
# 查詢需要遷移的數(shù)據(jù)庫表的數(shù)據(jù)條數(shù)
cur.execute('select count(*) from ' + table[0])
total = cur.fetchone()
page = total[0] / self.page_size
page1 = total[0] % self.page_size
if page1 != 0:
page = page + 1
# 阿里云數(shù)據(jù)庫創(chuàng)建表
cur_local.execute("SELECT table_name FROM information_schema.`TABLES` WHERE table_schema='user' AND table_name='" + str(table[0]).lower() + "'")
table_name = cur_local.fetchone()
if table_name is None:
cur_local.execute(create_sql)
for p in range(0, page):
while True:
try:
print '開始', table[0], '的第', p + 1, '頁查詢'
if p == 0:
limit_param = ' limit ' + str(p * self.page_size) + ',' + str(self.page_size)
else:
limit_param = ' limit ' + str(p * self.page_size + 1) + ',' + str(self.page_size)
cur.execute('select * from ' + table[0] + limit_param)
inserts = cur.fetchall()
print '查詢成功'
param = ''
for i in range(0, table_col_count[0]):
param = param + '%s,'
print '開始插入'
cur_local.executemany('replace into ' + table[0] + ' values (' + param[0:-1] + ')', inserts)
print table[0], '的第', p + 1, '頁, 插入完成, 還有', page - p - 1, '頁, 任重而道遠'
conn_local.commit()
break
except Exception as e:
print e
time.sleep(60)
cur = conn.cursor()
cur_local = conn_local.cursor()
print table[0], ' 插入完成'
print '\n \n ======================================================================== \n\n'
cur_local.close()
conn_local.close()
cur.close()
conn.close()
if __name__ == '__main__':
conn_mysql = ConnectMysql()
conn_mysql.getTable()以上就是python轉(zhuǎn)移數(shù)據(jù)庫里數(shù)據(jù)的方法,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 2.7,DELL G3電腦。
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:python如何轉(zhuǎn)移數(shù)據(jù)庫里的數(shù)據(jù)
文章起源:http://fisionsoft.com.cn/article/dpdogsj.html


咨詢
建站咨詢
