Log a MySQL implementation under Python that imports data from a TXT file

Source: Internet
Author: User
Tags pycharm community edition
Environment: python2.7

Comsenzxp comes with MySQL

Installing the Python-mysql module

Data format: Account information in TXT format.

Data is one line of data.

Difficulties: Some lines only account, no password, some are empty lines, some lines at the end of the line have three quotation marks, some empty lines, some are not account password information.

Code implementation:

1 #!/usr/bin/env python2 #Encoding:utf-83 4 5 """6 @version:??7 @author: Elijahxb8 @contact: elijahxb@163.com9 @site:Ten @software: pycharm Community Edition One @file: main.py A @time: 2017/7/8 23:47 - """ - ImportMySQLdb the ImportOS -#ImportRe -  -CONN_IP ='127.0.0.1' +Conn_username ='Root' -Conn_password ='11111111' +Conn_database ='Qqdata' AConn_table ='Login' atConn_port = 3306 -  -Importpath = u"""D:\QQ Database""". Encode ("GBK") -Pattern ="[0-9,a-z,a-z]{4,12}" -Sumlist = [] - defGett (path): inFiledata = [] -Onedata = [] toFileList =os.listdir (path) +      forFileinchfilelist: -         Print "Processing Files ..."+file theWith open (Os.path.join (path,file),'R') as FH: *Lines =Fh.readlines () $          forIndex,lineinchEnumerate (lines):Panax Notoginseng             Print "processing {0} row data, progress {0}/{1}, ' {2} '". Format (Index,len (lines), Str (FLOAT ("%0.2f"% (float (index)/len (lines))) *100) +"%") -             ifLen (line) < 14: the                 Continue +             elif '"""' inchLine : Aline = Line.split ('"""') [1] thetext_l = Line.split (" ") +Username =Text_l[0] -passwd = Text_l[1].split ("\ n") [0] $             ifLen (username) < 4orLen (passwd) < 4: $                 Continue - onedata.append (username) -Onedata.append ("'"+ passwd +"'") the filedata.append (Tuple (onedata)) -Onedata = []WuyiFiledata = List (set (Filedata))#clears all duplicates in a file the sumlist.append (Tuple (filedata)) -     returnsumlist Wu  -  About  $conn = MySQLdb.connect (host =Conn_ip, -user =Conn_username, -passwd =Conn_password, -db =Conn_database, APort =Conn_port +                        ) theCur =conn.cursor () -Cur.execute ("Use qqdata") $Cur.execute ("TRUNCATE TABLE Login") thesqlcmd ="INSERT INTO login (QQ,PWD) VALUES (%s,%s)" thet =Gett (Importpath) the  forSinglefiledatainchT: the Cur.executemany (sqlcmd,singlefiledata) - cur.close () inConn.close ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.