This article is an example of 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 for some time python the first time to write something with Python2.7
#!/usr/bin/python
#coding =utf-8
import _mysql,sys,io
def 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 ()
I hope this article will help you with your Python programming.