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

Posted 会吃键盘的小懒熊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的配置文件读取,日志文件写入,批量写入mysql(python2.7,win7)相关的知识,希望对你有一定的参考价值。

# -*- coding: utf-8 -*-
import log_config
import ConfigParser
import pymysql
logger = 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变量,list
def 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=root
password=root
database=liping
host=localhost
port =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;






























































































以上是关于python的配置文件读取,日志文件写入,批量写入mysql(python2.7,win7)的主要内容,如果未能解决你的问题,请参考以下文章

在使用 python 写入时从日志文件中读取

Python Configparser模块读取写入配置文件

腾讯云Logstash实战5-读取COS中的日志文件并写入到Elasticsearch

python读取/写入配置文件ini方法

flume系列之:python读取flume配置文件,并把配置写入到zookeeper节点,再根据写入到zookeeper中的配置启动flume agent

Python读取配置文件(config.ini),写入配置文件