V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chenqh
V2EX  ›  Python

关于 Python 导出 excel 快慢的问题

  •  
  •   chenqh · 2020-02-02 16:20:33 +08:00 · 3648 次点击
    这是一个创建于 1517 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在导出 excel, 大小 9m,接近 10m, 数据 接近 4w 条,居然要 40S,而且我感觉我也没有做什么呀

    1. 使用xlwt这个库
    2. 定义一个 headers, 元素为("cell 名字", "对应的字段", "宽度")
    3. 从数据库查询数据,转换一下时间格式,做了一些整除操作, 等等
      按理来讲不会这么慢呀!!
    第 1 条附言  ·  2020-02-03 18:28:08 +08:00

    body_producer代码

    
    def convert_list(li, datetime_format=consts.china_full_datetime_format,
                     zone_name=consts.default_zone_name):
        out = []
        for ele in li:
            out.append(ele.to_json(datetime_format,
                                   zone_name=zone_name,
                                   request_handler=None))
        return out
    
    
    def body_producer(page_size=1000):
        chunk = []
        for ele in query_result:
            chunk.append(ele)
            if len(chunk) > page_size:
    
                yield from convert_list(chunk)
                chunk = []
        if chunk:
            yield from convert_list(chunk)
    

    profile结果

             35238157 function calls (35232213 primitive calls) in 51.559 seconds
    
       Ordered by: cumulative time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        324/1    0.025    0.000   51.559   51.559 {built-in method builtins.exec}
            1    1.124    1.124   51.559   51.559 tmp.py:16(<module>)
        39631    0.113    0.000   27.180    0.001 tmp.py:37(body_producer)
       594465    0.856    0.000   15.790    0.000 Worksheet.py:1035(write)
       594465    3.486    0.000   14.142    0.000 Row.py:228(write)
            1    0.000    0.000   11.857   11.857 peewee.py:6729(__iter__)
            1    0.000    0.000   11.857   11.857 peewee.py:1839(inner)
            1    0.000    0.000   11.857   11.857 peewee.py:1914(execute)
            1    0.000    0.000   11.857   11.857 peewee.py:2085(_execute)
            1    0.000    0.000   11.857   11.857 peewee.py:3067(execute)
    

    看样子是是因为peewee的问题,但是感觉xlwt还是慢呀

    26 条回复    2020-03-03 10:00:12 +08:00
    suotm
        1
    suotm  
       2020-02-02 16:34:17 +08:00
    导出成 CSV,然后用 excel 导入?
    superrichman
        2
    superrichman  
       2020-02-02 16:42:45 +08:00 via iPhone
    帖代码出来,看你怎么写的
    chenqh
        3
    chenqh  
    OP
       2020-02-02 16:52:07 +08:00
    @superrichman 一般导出 4w 条,要多久?
    dlsflh
        4
    dlsflh  
       2020-02-02 16:57:01 +08:00 via Android   ❤️ 1
    贴代码吧,我一般用 pandas 做。
    jjx
        5
    jjx  
       2020-02-02 16:58:05 +08:00   ❤️ 1
    以前测算过, 具体数字我忘了, 是很慢

    换 openpxy writeonly 模式

    pypy 能明显提速

    性能比起 go 等静态语言来完全不能比
    jjx
        6
    jjx  
       2020-02-02 17:00:20 +08:00   ❤️ 1
    @jjx openpyxl 打错了
    1462326016
        7
    1462326016  
       2020-02-02 19:57:53 +08:00
    我觉得,问题应该修改为 使用 xlwt 导出 excel 慢
    wuwukai007
        8
    wuwukai007  
       2020-02-02 20:59:09 +08:00 via Android   ❤️ 1
    pandas 底层用的还是 xlwt,用 openpyxl
    alpha2016
        9
    alpha2016  
       2020-02-02 21:02:23 +08:00   ❤️ 1
    按一楼说的,写 csv 然后倒入 excel 呢
    chenqh
        10
    chenqh  
    OP
       2020-02-02 21:15:26 +08:00
    @alpha2016 这个怎么做?
    xiaoxinxiaobai
        11
    xiaoxinxiaobai  
       2020-02-02 21:19:21 +08:00 via Android
    起码分两个部分分析,数据转换耗时,数据写 excel 耗时,或者 profile 跑一下 看看哪部分最耗时
    chenqh
        12
    chenqh  
    OP
       2020-02-02 21:30:17 +08:00
    @xiaoxinxiaobai 这种网站的怎么 profile 呀,脚本我倒是会 profile
    renmu
        13
    renmu  
       2020-02-02 21:36:55 +08:00 via Android
    pandas read_sql () 然后直接 to_excel,应该最方便了
    ebingtel
        14
    ebingtel  
       2020-02-02 21:54:04 +08:00
    边查询、边返回吧……同样慢,但是会缩短 TTFT 用户体验会好
    chenqh
        15
    chenqh  
    OP
       2020-02-02 22:21:23 +08:00
    @renmu 关键我有一些转换的呀
    so1n
        16
    so1n  
       2020-02-02 22:25:53 +08:00 via Android
    导 csv 再放入 excel 最好,速度快,占内存低……
    youthfire
        17
    youthfire  
       2020-02-02 22:35:39 +08:00
    用 pandas+sqlite。我这边一个 32mb,40 万行,作查询和简单处理,基本都是在 2 秒内。当然索引肯定得做。上面有朋友说 pandas 是调用 engine,实际不如直接用 openpyxl,这个结论我亲测过,未必是这样,你可以自己试试。
    zwy100e72
        18
    zwy100e72  
       2020-02-02 22:39:05 +08:00
    建议楼主拿出 Pycharm,跑一次 profile 性能测试,你就能更好的评估 40s 究竟花在了什么地方
    有了相关数据才好决策到底是要换什么
    xiaoxinxiaobai
        19
    xiaoxinxiaobai  
       2020-02-02 22:49:10 +08:00 via Android
    我理解这是个接口调用?在接口代码里添加些 profile 代码,保存结果到文件然后分析下,可以搜一些例子
    mituxiaomanong
        20
    mituxiaomanong  
       2020-02-03 01:38:39 +08:00 via Android
    一般几十万数据几秒就可以了,检查数据查询部分和构建 excel 部分循环是否有多余代码
    chenqh
        21
    chenqh  
    OP
       2020-02-03 10:04:36 +08:00 via Android
    接口里面这种怎么做?
    jinliming2
        22
    jinliming2  
       2020-02-03 10:18:17 +08:00 via iPhone   ❤️ 1
    @chenqh 你如果不知道接口里怎么做的话,你可以单独写个脚本,只保留查询、转换并导出 excel 的代码,然后单独分析这一个脚本
    chenqh
        23
    chenqh  
    OP
       2020-02-03 10:31:53 +08:00 via Android
    @jinliming2 我试一试
    wuwukai007
        24
    wuwukai007  
       2020-02-04 22:58:16 +08:00
    读个 4w 条数据要十几秒,是远程数据库把,还是那种限速的那种?
    chenqh
        25
    chenqh  
    OP
       2020-02-05 00:24:21 +08:00 via Android
    @wuwukai007 本地,虚拟机的,程序也在虚拟机里面
    15399905591
        26
    15399905591  
       2020-03-03 10:00:12 +08:00
    我也碰到了这个问题, 使用 xlwt 写入 xls 文件非常慢,我使用的方法是使用 open 写入 csv,这样快很多,如果楼主找到了更好的解决办法,也发出来一下呗。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2850 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:44 · PVG 19:44 · LAX 04:44 · JFK 07:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.