昨天寫小專案的時候遇到了一個需求:把txt文件的資料匯入到mysql資料庫中,開始本來想直接用Mysql Workbench匯入TXT檔案,但是最後發現不支援TXT匯入,結果我吧嗒吧嗒的去把TXT轉了Excel,拿到Linux上匯入的時候又發現了各種亂碼問題。
抱著沒有什麼是程式設計師幹不了的原則,我手寫了一個Python程式碼直接操作檔案進行匯入了。結果大概一萬多條的檔案,匯入時間大概兩分鐘。
下面是具體的程式碼:
- mysqlpython.py檔案: 自定義的連線mysql資料庫的類
- importtxt.py檔案: 讀TXT檔案並進行插入操作
- dict.txt檔案: 要操作的TXT檔案
mysqlpython.py檔案
from pymysql import *
class Mysqlpython:
def __init__(self,database,host="localhost",
user="root",password="123456",
charset="utf8",port=3306):
self.database = database
self.host = host
self.user = user
self.password = password
self.charset = charset
self.port = port
# 建立資料連線和遊標物件
def open(self):
self.db = connect(host=self.host,
user=self.user,
password=self.password,
port=self.port,
database=self.database,
charset=self.charset)
self.cur = self.db.cursor()
# 關閉遊標物件和資料庫連線物件
def close(self):
self.cur.close()
self.db.close()
# 執行sql命令
def zhixing(self,sql,L=[]):
self.open()
self.cur.execute(sql,L)
self.db.commit()
self.close()
# 查詢功能
def all(self,sql,L=[]):
self.open()
self.cur.execute(sql,L)
result = self.cur.fetchall()
return result
if __name__ == "__main__":
sqlh = Mysqlpython("dictionary")
sel = "select * from user"
r = sqlh.all(sel)
print(r)
複製程式碼
importtxt.py檔案
import re
import sys
from mysqlpython import Mysqlpython
sqlh = Mysqlpython("dictionary")
def insert(data):
arr = data.split()
name = arr[0]
description = " ".join(arr[1:])
ins = "insert into words(name,description) values(%s,%s)"
sqlh.zhixing(ins,[name,description])
def get_addr():
f = open(`./dict.txt`)
lines=f.readlines()
for line in lines:
insert(line)
f.close()
return ``
if __name__ ==`__main__`:
print(get_addr())
複製程式碼
dict.py檔案(我複製了幾條檔案)
a indef art one
abacus n.frame with beads that slide along parallel rods, used for teaching numbers to children, and (in some countries) for counting
abandon v. go away from (a person or thing or place) not intending to return; forsake; desert
abandonment n. abandoning
abase v. ~ oneself/sb lower oneself/sb in dignity; degrade oneself/sb ;
abash to destroy the self-possession or self-confidence of:disconcert
abashed adj. ~ embarrassed; ashamed
abate v. make or become less
abattoir n. = slaughterhouse (slaughter)
複製程式碼
針對不同的分隔符修改一下正規表示式即可。全部程式碼都貼上去了,直接複製修改下資料庫的配置就可以執行了。