python的配置文件读取,日志文件写入,批量写入mysql(python2.7,win7)

# -*- coding: utf-8 -*-import log_configimport ConfigParserimport pymysqllogger = log_config.getlogger(‘getdataabdupdata‘, ‘getdataabdupdata.log‘)conf = ConfigParser.ConfigParser()conf.read("mysql.conf")user = conf.get("mysql", "user")password = conf.get("mysql", "password")database = conf.get("mysql", "database")host = conf.get("mysql", "host")port = conf.get("mysql", "port")

# 设置list变量,listdef start():    list = []    with open(‘notice_id.txt‘, ‘r‘)as file:        for one in file:            # 取到esID            esID = one.split(‘,‘)[0]            # 添加到list中            list.append(esID)            print list            logger.info(str(len(list)))            if len(list) >= 1000:                logger.info(‘start to update pro_status by es_ID 1000 pieces onetime‘)                updatepro_status(list)  # 更新数据库方法                list = []  # 清空list                continue

#  更新数据库,批量更新def updatepro_status(list):    conn = pymysql.connect(host=host, port=int(port), user=user, passwd=password, db=database)    try:        cursor = conn.cursor()        sql = ‘update ssb_insight_notice set pro_status = 1 where es_id in (%s)‘%‘,‘.join([‘%s‘]*len(list))        print sql        effect_row=cursor.execute(sql, list)        conn.commit()        logger.info("already into dabatabase %s" % effect_row)    finally:        conn.close()
start()

--------------------------------------------------------------------------------------------------------------------------------------------------mysql.conf
[mysql]user=rootpassword=rootdatabase=lipinghost=localhostport =3306
--------------------------------------------------------------------------------------------------------------------------------------------------
notice_id.txt
1205799084,notice/file/1205799084.PDF;1205798435,notice/file/1205798435.PDF;1205798417,notice/file/1205798417.PDF;1205797671,notice/file/1205797671.PDF;1205798441,notice/file/1205798441.PDF;1205798636,notice/file/1205798636.PDF;1205798358,notice/file/1205798358.PDF;1205797800,notice/file/1205797800.PDF;1205797904,notice/file/1205797904.PDF;1205798273,notice/file/1205798273.PDF;1205798572,notice/file/1205798572.PDF;1205798437,notice/file/1205798437.PDF;1205798541,notice/file/1205798541.PDF;1205797788,notice/file/1205797788.PDF;1205798423,notice/file/1205798423.PDF;1205798161,notice/file/1205798161.PDF;1205797841,notice/file/1205797841.PDF;1205798257,notice/file/1205798257.PDF;1205798584,notice/file/1205798584.PDF;1205797924,notice/file/1205797924.PDF;1205798384,notice/file/1205798384.PDF;1205798558,notice/file/1205798558.PDF;1205797464,notice/file/1205797464.PDF;1205798246,notice/file/1205798246.PDF;1205798259,notice/file/1205798259.PDF;1205797819,notice/file/1205797819.PDF;1205797790,notice/file/1205797790.PDF;1205798427,notice/file/1205798427.PDF;1205797624,notice/file/1205797624.PDF;1205797514,notice/file/1205797514.PDF;1205798108,notice/file/1205798108.PDF;1205798191,notice/file/1205798191.PDF;1205798307,notice/file/1205798307.PDF;1205798410,notice/file/1205798410.PDF;1205798658,notice/file/1205798658.PDF;1205797474,notice/file/1205797474.PDF;1205797738,notice/file/1205797738.PDF;

原文地址:https://www.cnblogs.com/sunmoon1993/p/10383524.html

时间: 2024-08-05 04:06:58

python的配置文件读取,日志文件写入,批量写入mysql(python2.7,win7)的相关文章

Python中将打印输出导向日志文件

Python中将打印输出导向日志文件 a. 利用sys.stdout将print行导向到你定义的日志文件中,例如: import sys # make a copy of original stdout route stdout_backup = sys.stdout # define the log file that receives your log info log_file = open("message.log", "w") # redirect pri

深入学习python解析并读取PDF文件内容的方法

这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应用.主要参考了一些已有的博客内容,代码. 主要思路是首先利用一个做项目的形式,描述所做的问题,运行环境,和需要安装的库,然后写代码,此代码是在python2.7中运行,然后写出在python3.6中运行的代码,并详细解释python2.7和python3.6中python库的一些不同之处,最后详细的

python代理池的构建1——代理IP类的构建,以及配置文件、日志文件、requests请求头

一.整体结构 二.代理IP类的构建(domain.py文件) ''' 实现_ init_ 方法, 负责初始化,包含如下字段: ip: 代理的IP地址 port:代理IP的端口号 protocol: 代理IP支持的协议类型,http是0, https是1, https和http都支持是2 nick_ type: 代理IP的匿名程度,高匿:0,匿名: 1,透明:2 speed:代理IP的响应速度,单位s area:代理IP所在地区 score:代理IP的评分,用于衡量代理的可用性;默认分值可以通过配

Selenium+Python参数化:读取TXT文件

概述 从Selenium模块化一文中,可以看出参数化的必要性,本文来介绍下读取外部txt文件的方法. 如何打开文件 打开文件有以下两个函数可以应用: 1.open(file_name,access_mode) file_name: 文件路径及名称: access_mode :访问方式,具体参数如下,,未提供参数,则默认为r: r:表示读取: w:表示写入: a:表示添加: +: 表示读写: b:表示2进制访问; 2.file函数 file()内建函数它的功能等于open(),如下根据文档说明可知

flume读取日志文件并存储到HDFS

配置hadoop环境 配置flume环境 配置flume文件 D:\Soft\apache-flume-1.8.0-bin\conf 将 flume-conf.properties.template 重新命名为  hdfs.properties # 组装 agenta1.sources = s1a1.channels = c1a1.sinks = k1 # 配置source:从目录中读取文件a1.sources.s1.type = spooldira1.sources.s1.channels =

通过python中xlrd读取excel表格(xlwt写入excel),xlsxwriter写入excel表格并绘制图形

1 import xlrd, xlwt 2 3 #读取excel文件 4 def read_excel(url):#传入源文件读取路径 5 # 获取数据 6 data = xlrd.open_workbook(url) 7 # 获取sheet 8 # table = data.sheet_by_name(sheet_name) #通过sheet名称获取sheet数据 9 table = data.sheet_by_index(0) #通过sheet索引获取sheet数据 10 # 获取总行数 1

实时读取日志文件

需求:在生成日志文件的过程中,需要实时读取该日志,需要每次读取时记录一次读取的位置,下一次从该位置读取 参考:http://sunnylocus.iteye.com/blog/694666 问题:由于我是每次读取几十行日志就停止一次,如果使用上面连接的代码RandomAccessFile.length()记录文件位置,发现记录的位置远大于读取的位置,这样为导致丢失一部分信息 方法1:因为日志是一直在写,一开始从0开始,可以先记录一下当前读取位置的的指针,下一次开始读的时候从该处再次读取 方法1需

Java读取excel文件,并存入MySQL数据库

2019,刚毕业入职,需要更新数据库某表内容,就写了个Java读取excel文件的代码,代码尚存问题较大,过往阅者看看即可,以此记录小白点滴 初学Java,还没学到io流,jdbc等操作 代码用到poi 一些jar,数据库jar import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.s

Java读取txt文件和覆盖写入txt文件和追加写入txt

package thread; import java.io.*; public class hh { /** * 创建文件 * @param fileName * @return */ public static boolean createFile(File fileName)throws Exception{ try{ if(!fileName.exists()){ fileName.createNewFile(); } }catch(Exception e){ e.printStackT