This example describes how Python imports txt data to MySQL. Share to everyone for your reference. The specific analysis is as follows:
Convert data from txt text to MySQL database, contact Python for the first time to write something with a Python2.7
#!/usr/bin/python#coding=utf-8import _mysql,sys,iodef addcity (prov,city,tel,post): try: conn=_ Mysql.connect ("192.168.1.99", ' php ', ' php '); Conn.query ("Set names UTF8"); Conn.query ("INSERT INTO ' domain '. ' Postcode ' (Province,city,telcode, postcode) VALUES ('" +prov+ "', '" +city+ "', '" + tel+ "', '" +post+ "')"); Result=conn.use_result (); #print ("version:%s"% result.fetch_row () [0]) conn.close () except _mysql. Error,e: print ("Error%d:%s"% (e.args[0],e.args[1)) sys.exit (1) If __name__== "__main__": f = open (" Data.txt "," R ") for line in F: content=line.split (", "); Print content[0],content[2] addcity (content[0],content[1],content[2],content[3]); F.close ()
Hopefully this article will help you with Python programming.