
Python連接MySQL并使用fetchall()方法過濾特殊字符
來一個簡單的例子,看Python如何操作數(shù)據(jù)庫,相比Java的JDBC來說,確實非常簡單,省去了很多復(fù)雜的重復(fù)工作,只關(guān)心數(shù)據(jù)的獲取與操作。
準(zhǔn)備工作
需要有相應(yīng)的環(huán)境和模塊:
Ubuntu 14.04 64bit
Python 2.7.6
MySQLdb
注意:Ubuntu 自帶安裝了Python,但是要使用Python連接數(shù)據(jù)庫,還需要安裝MySQLdb模塊,安裝方法也很簡單:
sudo apt-get install MySQLdb
然后進入Python環(huán)境,import這個包,如果沒有報錯,則安裝成功了:
python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
Python標(biāo)準(zhǔn)的數(shù)據(jù)庫接口的Python DB-API(包括Python操作MySQL)。大多數(shù)Python數(shù)據(jù)庫接口堅持這個標(biāo)準(zhǔn)。不同的數(shù)據(jù)庫也就需要不同額模塊,由于我本機裝的是MySQL,所以使用了MySQLdb模塊,對不同的數(shù)據(jù)庫而言,只需要更改底層實現(xiàn)了接口的模塊,代碼不需要改,這就是模塊的作用。
Python數(shù)據(jù)庫操作
首先我們需要一個測試表
建表語句:
CREATE DATABASE study;
use study;
DROP TABLE IF EXISTS python_demo;
CREATE TABLE python_demo (
id int NOT NULL AUTO_INCREMENT COMMENT '主鍵,自增',
user_no int NOT NULL COMMENT '用戶編號',
user_name VARBINARY(50) NOT NULL COMMENT '用戶名',
password VARBINARY(50) NOT NULL COMMENT '用戶密碼',
remark VARBINARY(255) NOT NULL COMMENT '用戶備注',
PRIMARY KEY (id,user_no)
)ENGINE =innodb DEFAULT CHARSET = utf8 COMMENT '用戶測試表';
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1001,'張三01','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1002,'張三02','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1003,'張三03','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1004,'張三04','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1005,'張三05','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1006,'張三06','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1007,'張三07','admin','我是張三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
(1008,'張三08','admin','我是張三');
Python代碼
# --coding=utf8--
import ConfigParser
import sys
import MySQLdb
def init_db():
try:
conn = MySQLdb.connect(host=conf.get('Database', 'host'),
user=conf.get('Database', 'user'),
passwd=conf.get('Database', 'passwd'),
db=conf.get('Database', 'db'),
charset='utf8')
return conn
except:
print "Error:數(shù)據(jù)庫連接錯誤"
return None
def select_demo(conn, sql):
try:
cursor = conn.cursor()
cursor.execute(sql)
return cursor.fetchall()
except:
print "Error:數(shù)據(jù)庫連接錯誤"
return None
def update_demo():
pass
def delete_demo():
pass
def insert_demo():
pass
if __name__ == '__main__':
conf = ConfigParser.ConfigParser()
conf.read('mysql.conf')
conn = init_db()
sql = "select * from %s" % conf.get('Database', 'table')
data = select_demo(conn, sql)
pass
fetchall()字段特殊字符過濾處理
最近在做數(shù)據(jù)倉庫的遷移工作,之前數(shù)據(jù)倉庫的數(shù)據(jù)都是用的shell腳本來抽取,后來換了python腳本.
但是在把數(shù)據(jù)抽取存放到hadoop時,出現(xiàn)了一個問題:
由于數(shù)據(jù)庫字段很多,提前也不知道數(shù)據(jù)庫字段會存儲什么內(nèi)容,hive建表是以\t\n做分隔,這就導(dǎo)致了一個問題,如果mysql字段內(nèi)容里面本身含有\(zhòng)t\n,那么就會出現(xiàn)字段錯位情況,并且很頭疼的是mysql有100多個字段,也不知道哪個字段會出現(xiàn)這個問題.
shell腳本里的做法是在需要抽取的字段上用mysql的replace函數(shù)對字段進行替換,例如,假設(shè)mysql里的字段是column1 varchar(2000),那么很可能就會出現(xiàn)有特殊字符的情況,在查詢的sql語句里加上
select replace(replace(replace(column1,'\r',''),'\n',''),'\t','')
之前一直是這么干的,但是這樣寫sql特別長,特別是有100多個字段,也不知道哪個有特殊字符,只要都加上.
所以在python中對字段不加處理,最終導(dǎo)致hive表字段對應(yīng)出現(xiàn)偏差,所以在python里從mysql查詢到的字段在寫到文件之前需要對每個字段進行過濾處理
看個例子,我就以mysql測試為例,首先建一張測試表
CREATE TABLE `filter_fields` (
`field1` varchar(50) DEFAULT NULL,
`field2` varchar(50) DEFAULT NULL,
`field3` varchar(50) DEFAULT NULL,
`field4` varchar(50) DEFAULT NULL,
`field5` varchar(50) DEFAULT NULL,
`field6` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
有六個字段,都是varchar類型,插入新數(shù)據(jù)可以在里面插入特殊字符.簡單插入條數(shù)據(jù)測試看看:
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test01','test02','test03','test04','test05','test06');
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test11\ntest11','test12\n\n','test13','test14','test15','test16');
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test21\ttest21','test22\ttest22\ttest22','test23\t\t\t','test4','test5','test6');
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test21\rest21','test22\r\rest22\r\rest22','test23\r\r\r','test4','test5','test6');
其中數(shù)據(jù)里插入的特殊字符,可能連在一起,也有不連在一起的.
python測試代碼:
# coding=utf-8
import MySQLdb
import sys
db_host = '127.0.0.1' # 數(shù)據(jù)庫地址
db_port = 3306 # 數(shù)據(jù)庫端口
db_user = 'root' # mysql用戶名
db_pwd = 'yourpassword' # mysql用戶密碼,換成你的密碼
db_name = 'test' # 數(shù)據(jù)庫名
db_table = 'filter_fields' # 數(shù)據(jù)庫表
# 過濾sql字段結(jié)果中的\t\n
def extract_data(table_name):
try:
conn = MySQLdb.connect(host=db_host, port = db_port, user=db_user,
passwd = db_pwd, db = db_name, charset = "utf8")
cursor = conn.cursor()
except MySQLdb.Error, e:
print '數(shù)據(jù)庫連接異常'
sys.exit(1)
try:
sql = 'select * from %s;'%(table_name)
cursor.execute(sql)
rows = cursor.fetchall()
print '====字段未過濾查詢結(jié)果===='
for row in rows:
print row
print '====字段過濾之后結(jié)果===='
rows_list = []
for row in rows:
row_list = []
for column in row:
row_list.append(column.replace('\t', '').replace('\n', '').replace('\r', ''))
rows_list.append(row_list)
print rows_list[-1] # [-1]表示列表最后一個元素
return rows_list
except MySQLdb.Error, e:
print '執(zhí)行sql語句失敗'
cursor.close()
conn.close()
sys.exit(1)
if __name__ == '__main__':
print 'begin:'
rows = extract_data(db_table)
pass
看看輸出結(jié)果:
字段未過濾查詢結(jié)果
(u'test01', u'test02', u'test03', u'test04', u'test05', u'test06')
(u'test11\ntest11', u'test12\n\n', u'test13', u'test14', u'test15', u'test16')
(u'test21\ttest21', u'test22\ttest22\ttest22', u'test23\t\t\t', u'test4', u'test5', u'test6')
(u'test21\rest21', u'test22\r\rest22\r\rest22', u'test23\r\r\r', u'test4', u'test5', u'test6')
字段過濾之后結(jié)果
[u'test01', u'test02', u'test03', u'test04', u'test05', u'test06']
[u'test11test11', u'test12', u'test13', u'test14', u'test15', u'test16']
[u'test21test21', u'test22test22test22', u'test23', u'test4', u'test5', u'test6']
[u'test21est21', u'test22est22est22', u'test23', u'test4', u'test5', u'test6']
可以看到,制表符,換行符,回車都被過濾了.
建議:最后說點題外話,不要小視\r,回車符.很多人以為回車符就是換行符,其實不是的,\r表示回車符,\n表示新行.之前代碼里其實是過濾掉了\t\n的,但是抽取的數(shù)據(jù)還是不對,后來看了源碼之后才發(fā)現(xiàn),原來是沒有過濾\r,就這個不同導(dǎo)致了很多數(shù)據(jù)抽取不對.
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動決策的時代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計的實用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點,而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報考到取證的全攻略? 在數(shù)字經(jīng)濟蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗:捕捉數(shù)據(jù)背后的時間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗:數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨特的門控機制,在 ...
2025-07-07統(tǒng)計學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費者需求的重要途徑,而統(tǒng)計學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03