shenghuotaiai
Python 第一次用,做个记录
import textwrap import pymssql
from datetime import datetime, timedelta class Data: # 数据库连接 server = "" user = "" password = \'\' database = \'\' list = [] # 定义列表用来存放SQL语句 def __init__(self, LogFileName): self.LogFileName = LogFileName # 格林时间转北京时间 def UTCtoBJS(self, UTC): # UTC_format = "%Y-%m-%dT%H:%M:%S.%fZ" BJS_format = "%Y-%m-%d %H:%M:%S" UTC = datetime.strptime(UTC, BJS_format) # 格林威治时间+8小时变为北京时间 BJS = UTC + timedelta(hours=8) BJS = BJS.strftime(BJS_format) return BJS # 批量导入数据库 def StoreToDatabase(self): try: connect = pymssql.connect(self.server, self.user, self.password, self.database) # 建立连接 if connect: print("连接成功!") cursor = connect.cursor() # 创建一个游标对象,python里的sql语句都要通过cursor来执行 sql = \'\' # 循环拼接SQL for items in self.list: sql += textwrap.dedent(items) cursor.execute(sql) # 执行sql语句 connect.commit() # 提交 self.list.clear() # 清空 list cursor.close() connect.close() else: print("连接失败!") except BaseException as ex: print(\'抛出异常\', ex) def main(self): f = open(self.LogFileName, \'r\', encoding=\'UTF-8\', errors=\'replace\') lines = f.readlines() self.list.clear() # list全局变量清空 print(\'开始执行,打印文件名\', self.LogFileName) for line in lines: if SourceSite in line: array = line.split(\' \', 5) AccessDate = self.UTCtoBJS(\'%s %s\' % (array[0], array[1])) # 时间 SystemPage = array[4][1:len(array[4])] if \'App Store\' in SystemPage: # txt记录一份 可直接删除 # with open(\'lucky.txt\', "a", encoding=\'UTF-8\') as file: # ”w"代表着每次运行都覆盖内容 # file.write( # \'%s %s %s %s \n\' % (time, array[2], array[3], myPageURL)) sql = "INSERT dbo.ChemicalbookAccessData (AccessDate,SourceSite,SystemPage,CreateDate)VALUES(\\'%s\\',\\'%s\\',\\'%s\\',GETDATE());" % ( AccessDate, SourceSite, SystemPage) self.list.append(sql) # list 存储 SQL 每20条执行一次 if len(self.list) == 20: self.StoreToDatabase() print(\'每次导入20条数据,导入成功\') # 再来一个导入 if len(self.list) > 0: print(\'再来一个导入%s条数据\' % (len(self.list))) self.StoreToDatabase() print(\'最后一次导入成功\') print(\'------------程序结束执行---------------\') if __name__ == \'__main__\': dateNow = datetime.now().strftime(\'u_ex%C%m%d.log\') # 生成文件名 mm = ChemicalbookAccessData(\'D:/Projects/\' + dateNow) # 直接传路径 mm.main()

 

分类:

技术点:

相关文章:

  • 2021-06-16
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-11-23
  • 2021-04-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-08
  • 2021-07-04
  • 2021-11-06
  • 2021-12-11
  • 2021-12-10
  • 2022-12-23
相关资源
相似解决方案