python文件讀寫并使用mysql批量插入示例分享(python操作mysql)
# -*- coding: utf-8 -*-
'''
Created on 2013年12月9日
@author: hhdys
'''
import os
import mysql.connector
config = {
'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()
相關(guān)文章
python3列表刪除大量重復(fù)元素remove()方法的問題詳解
這篇文章主要給大家介紹了關(guān)于python3列表刪除大量重復(fù)元素remove()方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于python select.select模塊通信的實例講解
下面小編就為大家?guī)硪黄趐ython select.select模塊通信的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Python中for循環(huán)和while循環(huán)的基本使用方法
這篇文章主要介紹了Python中for循環(huán)和while循環(huán)的基本使用方法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08Python 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)-循環(huán)隊列的操作方法
這篇文章主要介紹了Python 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)-循環(huán)隊列的操作方法,需要的朋友可以參考下2019-07-07python入門學(xué)習(xí)關(guān)于for else的特殊特性講解
本文將介紹 Python 中的" for-else"特性,并通過簡單的示例說明如何正確使用它,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11