昨天写小项目的时候遇到了一个需求:把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)
复制代码
针对不一样的分隔符修改一下正则表达式便可。所有代码都贴上去了,直接复制修改下数据库的配置就能够运行了。正则表达式