Use Python to import txt data to mysql. Use python to import txtmysql
This example describes how to import txt data to mysql using Python. Share it with you for your reference. The specific analysis is as follows:
From converting TXT text to MYSQL database, python used Python2.7 for the first time.
#! /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 () into T _ 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 Python programming.