Python导入txt数据到mysql的方法

WBOY
Release: 2016-06-06 11:23:55
Original
1309 people have browsed it

本文实例讲述了Python导入txt数据到mysql的方法。分享给大家供大家参考。具体分析如下:

从TXT文本转换数据到MYSQL数据库,接触一段时间python了 第一次写东西 用的是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()
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template