• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python文件读写并使用mysql批量插入示例分享(python操作mysql)

python文件读写并使用mysql批量插入示例分享(python操作mysql)

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python简单代码示例,python示例程序,python代码示例,python示例,python爬虫示例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

@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()
</div>

</div>

您可能想查找下面的文章:

  • Python编程生成随机用户名及密码的方法示例
  • Python判断变量是否为Json格式的字符串示例
  • python实现逻辑回归的方法示例
  • python分割列表(list)的方法示例
  • Python编程生成随机用户名及密码的方法示例
  • Python判断变量是否为Json格式的字符串示例
  • python实现逻辑回归的方法示例
  • Python实现对字符串的加密解密方法示例
  • Python列表切片用法示例
  • python利用拉链法实现字典方法示例

相关文章

  • wxpython中利用线程防止假死的实现方法
  • Python 模块EasyGui详细介绍
  • Python中的super用法详解
  • python返回昨天日期的方法
  • python安装mysql-python简明笔记(ubuntu环境)
  • python实现图片变亮或者变暗的方法
  • Windows系统下使用flup搭建Nginx和Python环境的方法
  • python self,cls,decorator的理解
  • Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
  • 浅析Python的web.py框架中url的设定方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python搭建HTTP服务器和FTP服务器
    • python实现统计代码行数的方法
    • 双向RNN:bidirectional_dynamic_rnn()函数的使用详解
    • 使用PyCharm配合部署Python的Django框架的配置纪实
    • python 打印对象的所有属性值的方法
    • Python urlopen 使用小示例
    • Python 爬虫学习笔记之单线程爬虫
    • Python回调函数用法实例详解
    • 在Python中定义和使用抽象类的方法
    • python脚本爬取字体文件的实现方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有