大蟒蛇python教程共享Python+pandas编写命令行脚本操作excel的tips详情

一、python logging日志模块简单封装

项目根目录创建 utils/logutil.py

import logging  from logging.handlers import timedrotatingfilehandler  from logging.handlers import rotatingfilehandler  class log(object):      stand = "stand"   # 输出到控制台      file = "file"     # 输出到文件      all = "all"       # 输出到控制台和文件        def __init__(self, mode=stand):          self.log_format = "%(asctime)s - %(levelname)s - %(message)s"          self.logger = logging.getlogger()          self.init(mode)      def debug(self, msg):          self.logger.debug(msg)      def info(self, msg):          self.logger.info(msg)      def warning(self, msg):          self.logger.warning(msg)      def error(self, msg):          self.logger.error(msg)      def init(self, mode):          self.logger.setlevel(logging.debug)          if mode == "stand":              # 输出到控制台 ------              self.stand_mode()          elif mode == "file":              # 输出到文件 --------              self.file_mode()          elif mode == "all":              # 输出到控制台和文件              self.stand_mode()              self.file_mode()      def stand_mode(self):          stand_handler = logging.streamhandler()          stand_handler.setlevel(logging.debug)          stand_handler.setformatter(logging.formatter(self.log_format))          self.logger.addhandler(stand_handler)        def file_mode(self):          '''          filename:日志文件名的prefix;          when:是一个字符串,用于描述滚动周期的基本单位,字符串的值及意义如下:           “s”: seconds           “m”: minutes           “h”: hours           “d”: days           “w”: week day (0=monday)           “midnight”: roll over at midnight          interval: 滚动周期,单位有when指定,比如:when='d',interval=1,表示每天产生一个日志文件;          backupcount: 表示日志文件的保留个数;          '''          # 输出到文件 -----------------------------------------------------------          # 按文件大小输出          # file_handler = rotatingfilehandler(filename="my1.log", mode='a', maxbytes=1024 * 1024 * 5, backupcount=10, encoding='utf-8')  # 使用rotatingfilehandler类,滚动备份日志          # 按时间输出          file_handler = timedrotatingfilehandler(filename="my.log", when="d", interval=1, backupcount=10,                                                  encoding='utf-8')          file_handler.setlevel(logging.debug)          file_handler.setformatter(logging.formatter(self.log_format))          self.logger.addhandler(file_handler)  log = log(mode=log.stand)

使用方法:

from utils.logutil import log  if __name__ == '__main__':      log.debug("debug msg")      log.info("info msg")      log.warning("warning msg")      log.error("error msg")

跑一下测试结果:

Python+pandas编写命令行脚本操作excel的tips详情

二、pandas编写命令行脚本操作excel的小tips

这里用上面日志小工具
如果不想用这个,可以把日志输出的代码换成 print() 或者删掉

1、tips

1.1使用说明格式

# 使用说明 -----------------------------------  time.sleep(1)  print('===========================================================')  print('简单说明:使用正则表达式拆分excel表中不规范的作者,初步提取对应需求字段')  print('ps:')  print('1.文件夹下需要有一个excel(只放一个,名称随意),其中一列“作者”保存着待拆分的作者')  print('2.拆分后的excel将新增几列拆分结果列,以 <作者>[拆分] 作为列名标记')  print('===========================================================')  time.sleep(1)  # ------------------------------------------

1.2接收操作目录方法

# 输入操作路径 ----------------------------------------------------------------  operate_dir = input('请输入excel目录(旺柴):')  # d:pycharmprojectsspiders图片下载工具excel  operate_dir = os.path.abspath(operate_dir)  # operate_dir = os.path.abspath(r'c:userscxstar46desktop正则表达式题名拆分测试')  # -----------------------------------------------------------------------------

1.3检测并读取目录下的excel,并限制当前目录只能放一个excel

# 检测excel数量,只能放一个,当只有一个excel时,提取它的路径excel_path -------  log.info('检查路径下的文件格式...')  excel_name = none  excel_count = 0  file_list = os.listdir(operate_dir)  for file in file_list:      if file.endswith('.xlsx') or file.endswith('.xlx'):          excel_count += 1          excel_name = file  if excel_count == 0:      log.error('文件夹下没有excel')      input('按任意键退出...')      raise exception(0)  if excel_count > 1:      log.error("无法读取excel,文件夹下有多个excel,或者excel未关闭")      input('按任意键退出...')      raise exception(0)    # print(excel_name)  # raise exception(1212)  # ----------------------------------------------------------------------  # print(excel_path)  # print(img_dir)    # 读取excel ----------------------------------------  excel_path = os.path.join(operate_dir, excel_name)  # print(excel_path)  try:      df = pd.read_excel(excel_path)      df = df.where(df.notnull(), none)  except exception as e:      log.error(e)      log.error('文件不存在或已损坏...')      input('按任意键退出...')      raise exception(0)  # -------------------------------------------------    # 打印excel行数  print(df.shape[0])

1.4备份excel

# 备份原始excel表 --------------------------------------------------------------  log.info('备份excel...')  bak_dir = '封面上传前的备份'   # 备份文件夹的名称  file_list = os.listdir(operate_dir)  if bak_dir not in file_list:      os.makedirs(os.path.join(operate_dir, bak_dir))  bak_excel_path = os.path.join(os.path.join(operate_dir, bak_dir), "{}_{}".format(datetime.datetime.now().strftime("%y-%m-%d_%h-%m-%s"), excel_name))  shutil.copyfile(excel_path, bak_excel_path)  # -----------------------------------------------------------------------------

1.5报错暂停,并显示异常信息

try:      raise exception("执行业务报错")  except exception as e:      import traceback      log.error(traceback.format_exc())	# 记录异常信息  input('执行完毕,按任意键继续...')

1.6判断excel是否包含某列,不包含就新建

cover_ruid_col_name = "封面ruid"    # 没有封面ruid这一列就创建  if cover_ruid_col_name not in df.columns.values:      df.loc[:, cover_ruid_col_name] = none

1.7进度展示与阶段保存

# 读取excel  excel_path = './封面上传测试.xlsx'  df = pd.read_excel(excel_path)  review_col = "审核结果"  # 没有“审核结果”这一列就创建  if review_col not in df.columns.values:      df.loc[:, review_col] = none  for i in range(df.shape[0]):    	# 打印进度 ---------------------------------------------      log.info("----------------------------------")      log.info("当前进度: {} / {}".format(i+1, df.shape[0]))      # ----------------------------------------------------  	# 执行表格插入业务  	# 判断业务  	# 吧啦吧啦  	# 业务执行结果插入原表  	df.loc[i, "审核结果"] = "好耶"        # 阶段性保存 ----------------------------      save_space = 200	# 每执行两百行保存一次      if i+1 % save_space == 0 and i != 0:          df.to_excel(excel_path, index=0)          log.info("阶段性保存...")      # -------------------------------------

到此这篇关于python+pandas编写命令行脚本操作excel的tips详情的文章就介绍到这了,更多相关python操作excel的tips内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多python教程分享Python+pandas编写命令行脚本操作excel的tips详情,都可以关注python教程分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/pythontutorial/1187814.html

(0)
上一篇 2022年8月4日
下一篇 2022年8月4日

精彩推荐