Linux中將txt導(dǎo)入到mysql的方法教程
昨天寫小項(xiàng)目的時(shí)候遇到了一個(gè)需求:把txt文檔的數(shù)據(jù)導(dǎo)入到mysql數(shù)據(jù)庫(kù)中,開始本來(lái)想直接用Mysql Workbench導(dǎo)入TXT文件,但是最后發(fā)現(xiàn)不支持TXT導(dǎo)入,結(jié)果我吧嗒吧嗒的去把TXT轉(zhuǎn)了Excel,拿到Linux上導(dǎo)入的時(shí)候又發(fā)現(xiàn)了各種亂碼問(wèn)題。
抱著沒(méi)有什么是程序員干不了的原則,我手寫了一個(gè)Python代碼直接操作文件進(jìn)行導(dǎo)入了。結(jié)果大概一萬(wàn)多條的文件,導(dǎo)入時(shí)間大概兩分鐘。
下面是具體的代碼:
- mysqlpython.py文件: 自定義的連接mysql數(shù)據(jù)庫(kù)的類
- importtxt.py文件: 讀TXT文件并進(jìn)行插入操作
- 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
# 創(chuàng)建數(shù)據(jù)連接和游標(biāo)對(duì)象
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()
# 關(guān)閉游標(biāo)對(duì)象和數(shù)據(jù)庫(kù)連接對(duì)象
def close(self):
self.cur.close()
self.db.close()
# 執(zhí)行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文件(我復(fù)制了幾條文件)
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)
針對(duì)不同的分隔符修改一下正則表達(dá)式即可。全部代碼都貼上去了,直接復(fù)制修改下數(shù)據(jù)庫(kù)的配置就可以運(yùn)行了。
總結(jié):
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)本站的支持。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信