博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python文件读取并使用mysql批量插入
阅读量:7108 次
发布时间:2019-06-28

本文共 1257 字,大约阅读时间需要 4 分钟。

hot3.png

# -*- coding: utf-8 -*-'''Created on 2013年12月9日@author: hhdys'''import osimport mysql.connectorconfig = {  'user': 'root',  'password': '******',  'host': '127.0.0.1',  'database': 'test',  'raise_on_warnings': True,}cnx = mysql.connector.connect(**config)class ReadFile:    def readLines(self):        f = open("E:/data/2013-11-5.txt", "r", 1, "utf-8")        i=0        list=[]        for line in f:            strs = line.split("\t")            if len(strs) != 5:                continue            data=(strs[0], strs[1], strs[2], strs[3], strs[4].replace("\n",""))            list.append(data)            cursor=cnx.cursor()            sql = "insert into data_test(uid,log_date,fr,is_login,url)values(%s,%s,%s,%s,%s)"            if i>5000:                cursor.executemany(sql,list)                cnx.commit()                print("插入")                i=0                list.clear()            i=i+1        if i>0:            cursor.executemany(sql,list)            cnx.commit()        cnx.close()        f.close()        print("ok")    def listFiles(self):        d = os.listdir("E:/data/")        return d                   if __name__ == "__main__":    readFile = ReadFile()    readFile.readLines()

转载于:https://my.oschina.net/hhdys412/blog/182762

你可能感兴趣的文章
第十章 集成ISIS协议--10.1
查看>>
六、C# 派生
查看>>
SSH整合错误三连
查看>>
如何把彩色网页快速变为灰色网页?
查看>>
[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:实现基本的CRUD功能
查看>>
NO24 第三关--企业面试题
查看>>
《Java技术》预备作业02 计科1502李宇蒙
查看>>
rnn lstm
查看>>
升级Chrome后无法打开网页
查看>>
软工第三次团队作业
查看>>
asp.net页面传值
查看>>
LINQ之路 1: LINQ介绍
查看>>
Find Integer HDU - 6441(费马大定理+勾股数)
查看>>
MYCAT分库分表
查看>>
Python基础知识(2)
查看>>
python 集合的操作方法
查看>>
nginx使用负载均衡进行压力测试得到的结论
查看>>
DELL PowerEdge R410系统日志满报错
查看>>
centos7 nginx图片 服务器可以访问ftp用户上传的图片资源的配置
查看>>
Git介绍,安装,Git+Git flow使用
查看>>