推荐学习书目
› 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
simple2025
0D
V2EX  ›  Python

关于 Python 导出 excel 快慢的问题

  •  
  •   simple2025 · Feb 2, 2020 · 5030 views
    This topic created in 2481 days ago, the information mentioned may be changed or developed.

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

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

    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 replies  •  2020-03-03 10:00:12 +08:00
    suotm
    suotm
    Feb 2, 2020
    1
    导出成 CSV,然后用 excel 导入?
    superrichman
    superrichman
    Feb 2, 2020 via iPhone
    2
    帖代码出来,看你怎么写的
    simple2025
    simple2025
    OP
    Feb 2, 2020
    3
    @superrichman 一般导出 4w 条,要多久?
    dlsflh
    dlsflh
    Feb 2, 2020 via Android❤️1
    4
    贴代码吧,我一般用 pandas 做。
    jjx
    jjx
    Feb 2, 2020❤️1
    5
    以前测算过, 具体数字我忘了, 是很慢

    换 openpxy writeonly 模式

    pypy 能明显提速

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