环境: python2.7
ComsenzXP自带MySQL
安装python-MySQL模块
数据格式:txt格式的账号信息。
数据一行一条数据。
立即学习“Python免费学习笔记(深入)”;
难点:有的行只有账号,没有密码;有的为空行;有的行首行尾有三连引号;有的空行;有的不是账号密码信息。
代码实现:
1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 5 """ 6 @version: ?? 7 @author: elijahxb 8 @contact: elijahxb@163.com 9 @site: 10 @software: PyCharm Community Edition11 @file: main.py12 @time: 2017/7/8 23:4713 """14 import MySQLdb15 import os16 #import re17 18 Conn_IP = '127.0.0.1'19 Conn_UserName = 'root'20 Conn_PassWord = '11111111'21 Conn_database = 'qqdata'22 Conn_Table = 'login'23 Conn_Port = 330624 25 importpath = u"""D:QQ数据库""".encode("gbk")26 pattern = "[0-9,a-z,A-Z]{4,12}"27 sumlist = []28 def gett(path):29 filedata = []30 onedata = []31 filelist = os.listdir(path)32 for file in filelist:33 print "处理文件中... ->" + file34 with open(os.path.join(path,file),'r') as fh:35 lines = fh.readlines()36 for index,line in enumerate(lines):37 print "正在处理第{0}行数据,进度{0}/{1},【{2}】".format(index,len(lines),str(float("%0.2f"%(float(index)/len(lines)))*100) + "%")38 if len(line) < 14:39 continue40 elif '"""' in line:41 line = line.split('"""')[1]42 text_l = line.split(" ")43 username = text_l[0]44 passwd = text_l[1].split("")[0]45 if len(username) < 4 or len(passwd) < 4:46 continue47 onedata.append(username)48 onedata.append("'" + passwd + "'")49 filedata.append(tuple(onedata))50 onedata = []51 filedata = list(set(filedata))#清除一个文件里面的所有重复项52 sumlist.append(tuple(filedata))53 return sumlist54 55 56 57 conn = MySQLdb.Connect(host = Conn_IP,58 user = Conn_UserName,59 passwd = Conn_PassWord,60 db = Conn_database,61 port = Conn_Port62 )63 cur = conn.cursor()64 cur.execute("use qqdata")65 cur.execute("truncate table login")66 sqlcmd = "insert into login (QQ,PWD) values(%s,%s)"67 t = gett(importpath)68 for singlefiledata in t:69 cur.executemany(sqlcmd,singlefiledata)70 cur.close()71 conn.close()
登录后复制
以上就是记录一次从txt文件导入数据的python下的MySQL实现的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2268362.html