昨天寫小項目的時候遇到了一個需求:把txt文檔的數據導入到mysql數據庫中,開始原本想直接用Mysql Workbench導入TXT文件,可是最後發現不支持TXT導入,結果我吧嗒吧嗒的去把TXT轉了Excel,拿到Linux上導入的時候又發現了各類亂碼問題。python
抱着沒有什麼是程序員幹不了的原則,我手寫了一個Python代碼直接操做文件進行導入了。結果大概一萬多條的文件,導入時間大概兩分鐘。mysql
下面是具體的代碼:程序員
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)
複製代碼
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())
複製代碼
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)
複製代碼
針對不一樣的分隔符修改一下正則表達式便可。所有代碼都貼上去了,直接複製修改下數據庫的配置就能夠運行了。正則表達式