脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python+pandas编写命令行脚本操作excel的tips详情

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

2022-07-28 17:39什么都干的派森 Python

这篇文章主要介绍了Python+pandas编写命令行脚本操作excel的tips详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)

使用方法:

?
1
2
3
4
5
6
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使用说明格式

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

1.2接收操作目录方法

?
1
2
3
4
5
# 输入操作路径 ----------------------------------------------------------------
operate_dir = input('请输入excel目录(旺柴):'# D:\PycharmProjects\spiders\图片下载工具\excel
operate_dir = os.path.abspath(operate_dir)
# operate_dir = os.path.abspath(r'C:\Users\cxstar46\Desktop\正则表达式题名拆分测试')
# -----------------------------------------------------------------------------

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 检测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

?
1
2
3
4
5
6
7
8
9
# 备份原始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报错暂停,并显示异常信息

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

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

?
1
2
3
4
5
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进度展示与阶段保存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 读取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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_43721000/article/details/126016051

延伸 · 阅读

精彩推荐
  • PythonPython实现FM算法解析

    Python实现FM算法解析

    这篇文章主要介绍了Python实现FM算法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    Bo_hemian9802021-07-15
  • Pythonpython基础之set集合详解

    python基础之set集合详解

    这篇文章主要介绍了python基础之set集合详解,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好地帮助,需要的朋友可以参考下...

    思想流浪者11832021-10-15
  • Pythonpython生成n个元素的全组合方法

    python生成n个元素的全组合方法

    今天小编就为大家分享一篇python生成n个元素的全组合方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    那时的风儿9412021-04-19
  • Python在Linux下使用命令行安装Python

    在Linux下使用命令行安装Python

    这篇文章主要介绍了在Linux下使用命令行安装Python,通过详细的图文介绍Linux安装Python的全部过程,希望对你有所帮助...

    象在舞10812021-12-08
  • PythonPython实现聊天机器人的示例代码

    Python实现聊天机器人的示例代码

    这篇文章主要介绍了Python实现聊天机器人,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    海天一树X20942021-03-14
  • Python使用Python设计一个代码统计工具

    使用Python设计一个代码统计工具

    这篇文章主要介绍了使用Python设计一个代码统计工具的相关资料,包括文件个数,代码行数,注释行数,空行行数。感兴趣的朋友跟随脚本之家小编一起看...

    PYTHON之禅9682021-01-27
  • PythonPython3 SSH远程连接服务器的方法示例

    Python3 SSH远程连接服务器的方法示例

    这篇文章主要介绍了Python3 SSH远程连接服务器的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    HFUT_qianyang5382021-05-09
  • Pythoncelery实现动态设置定时任务

    celery实现动态设置定时任务

    这篇文章主要为大家详细介绍了celery实现动态设置定时任务,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    特仑苏纯酸奶6302021-09-18