4秒读取50w行Excel数据

4秒读取50w行Excel数据

文章比较了几种常用的读取Excel的方法,最终发现rust库Calamine的速度最快,可以在4秒内读取50w行excel数据。

原文:Fastest Way to Read Excel in Python:https://hakibenita.com/fast-excel-python

我们在测试什么

我们创建了一个25MB的Excel文件(.xlsx),包含50w行数据,每行的内容包含整数、小数、日期、布尔值、字符串5列。

使用一个迭代器来逐行遍历文件,但不进行任何操作,只比较读取速度。

使用time.perf_counter()来记录时间。

python 复制代码
import time

start = time.perf_counter()
for row in iter_excel(file): pass
elapsed = time.perf_counter() - start

参与比较的方法

  • Pandas:Pandas是Python的数据分析库,
  • Tablib:Tablib 是 Python 中最受欢迎的库之一,用于导入和导出各种格式的数据。它最初是由requests库的创建者开发的。
  • Openpyxl:专门在Python中读写Excel数据的库。
  • LibreOffice:一个开源的办公软件,支持xlsx,并且提供了命令行模式。
  • DuckDB:DuckDB 是一个"进程内 SQL OLAP 数据库管理系统"
  • Calamine:Calamine 是一个纯 Rust 库,用于读取 Excel 和 OpenDocument 电子表格文件。python-calamine是它的Python绑定。

他们的运行结果如下:

方法 耗时(秒) 保留类型 版本
Pandas 32.98 Yes 2.1.3
Tablib 28.52 Yes 3.5.0
Openpyxl 35.62 Yes 3.1.2
Openpyxl (readonly) 24.79 Yes 3.1.2
LibreOffice 15.27 No 7.5.8.2
DuckDB (sql) 11.36 Yes 0.9.2
DuckDB (execute) 5.73 No 0.9.2
Calamine (python-calamine) 3.58 Yes 0.22.1 (0.1.7)

文章也提供了代码:https://github.com/hakib/fast-excel-python

我的运行结果和原文类似,calamine能在4秒(大概4秒,具体时间和电脑配置有关)完成50w行Excel数据的读取。尽管Python性能一般,但享受了Rust高性能的福利。

运行代码需要Python3.9及以上版本。

相关推荐
wtsolutions2 小时前
Real-World Use Cases - How Organizations Use Excel to JSON
json·github·excel
一只小H呀の2 小时前
pandas处理excel数据
excel·pandas
wregjru2 小时前
【操作系统】3.开发工具
excel
wtsolutions4 小时前
MCP Service Integration - Excel to JSON for AI and Automation
人工智能·json·excel
wtsolutions15 小时前
JSON to Excel Add-in - Seamless Integration Within Excel
json·excel
wtsolutions15 小时前
Getting Started with JSON to Excel Web App - Convert in Seconds
json·excel·web app
wtsolutions19 小时前
Using the JSON to Excel API - Programmatic Access for Developers
json·excel
qq_4351395719 小时前
EasyExcel(FastExcel)Excel导出功能 技术文档
excel
wtsolutions1 天前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions1 天前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel