python xlrd读取excel慢_与xlrd相比,使用openpyxl读取Excel文件要慢很多

news/2025/2/13 2:17:48/

我有一个Excel电子表格,需要每天导入到SQL Server中。电子表格将包含大约250000行,大约50列。我使用几乎相同的代码分别使用openpyxl和xlrd进行了测试。

下面是我正在使用的代码(减去调试语句):import xlrd

import openpyxl

def UseXlrd(file_name):

workbook = xlrd.open_workbook(file_name, on_demand=True)

worksheet = workbook.sheet_by_index(0)

first_row = []

for col in range(worksheet.ncols):

first_row.append(worksheet.cell_value(0,col))

data = []

for row in range(1, worksheet.nrows):

record = {}

for col in range(worksheet.ncols):

if isinstance(worksheet.cell_value(row,col), str):

record[first_row[col]] = worksheet.cell_value(row,col).strip()

else:

record[first_row[col]] = worksheet.cell_value(row,col)

data.append(record)

return data

def UseOpenpyxl(file_name):

wb = openpyxl.load_workbook(file_name, read_only=True)

sheet = wb.active

first_row = []

for col in range(1,sheet.max_column+1):

first_row.append(sheet.cell(row=1,column=col).value)

data = []

for r in range(2,sheet.max_row+1):

record = {}

for col in range(sheet.max_column):

if isinstance(sheet.cell(row=r,column=col+1).value, str):

record[first_row[col]] = sheet.cell(row=r,column=col+1).value.strip()

else:

record[first_row[col]] = sheet.cell(row=r,column=col+1).value

data.append(record)

return data

xlrd_results = UseXlrd('foo.xls')

openpyxl_resuts = UseOpenpyxl('foo.xls')

传递包含3500行的同一个Excel文件会产生截然不同的运行时间。使用xlrd我可以在2秒内将整个文件读入字典列表。使用openpyxl可以得到以下结果:Reading Excel File...

Read 100 lines in 114.14509415626526 seconds

Read 200 lines in 471.43183994293213 seconds

Read 300 lines in 982.5288782119751 seconds

Read 400 lines in 1729.3348784446716 seconds

Read 500 lines in 2774.886833190918 seconds

Read 600 lines in 4384.074863195419 seconds

Read 700 lines in 6396.7723388671875 seconds

Read 800 lines in 7998.775000572205 seconds

Read 900 lines in 11018.460735321045 seconds

虽然我可以在最后的脚本中使用xlrd,但由于各种问题(即int读作float,date读作int,datetime读作float),我将不得不硬编码许多格式。由于我需要为更多的导入重用此代码,因此尝试硬编码特定列以正确格式化它们是没有意义的,并且必须在4个不同的脚本中维护类似的代码。

对如何进行有什么建议吗?


http://www.ppmy.cn/news/812751.html

相关文章

关于excel打开缓慢,即使只是新建

问题描述 双击excel文件或者新建excel时,总要等半天(5s)才会有加载框出来,出来加载框后就正常了。 解决方式 尝试了百度的这个方法,打不开原有文件 还有一种方法类似,只不过改成了这个 /n “%1” /o “%u…

excel打开html非常慢,excel打开很慢,教您Excel表格打开后很卡很慢怎么办

在用Excel制作表格时,表格中输入了数据,并用了数学公式来对数据进行汇总统计。当放到其它计算机中打开时,却发现Excel表打开反应慢。那么,Excel表打开反应慢怎么办呢?下面,小编给大家讲解Excel表格打开后很…

为什么office Word/Excel打开特别慢?

经常发现很多人的office打开特别慢,有可能是这个原因导致的。 选择文件菜单中的 选项 并点加载 中的转到 把一些不必要的勾选掉 转载于:https://blog.51cto.com/11544823/2117770

【备战秋招】每日一题:2023-华为机试-比赛的冠亚季军

为了更好的阅读体检,可以查看我的算法学习网 本题在线评测链接:P1330 题目描述 有 N ( 3 ≤ N < 10000 ) N (3\leq N< 10000) N(3≤

使用MindStudio进行城市道路交通预测

一、前言 本来是想要比较STGCN和TGCN两个模型在SZ-Taxi数据集上的精度&#xff0c;但是实际操作下来发现STGCN在该数据集上的精度始终要低于TGCN&#xff0c;并且网上比较过两个模型精度的论文也显示如此&#xff08;https://arxiv.org/pdf/2103.06126.pdf&#xff09;&#x…

详细介绍ERNIE 3.0: Large-scale Knowledge Enhanced Pre-training for Language Understanding and Generation

系列阅读&#xff1a; 详细介绍百度ERNIE1.0&#xff1a;Enhanced Representation through Knowledge Integration 详细介绍百度ERNIE 2.0&#xff1a;A Continual Pre-Training Framework for Language Understanding 详细介绍百度ERNIE 3.0: Large-scale Knowledge Enhanced …

本地部署Stable Diffusion Webui AI 记录

Stable Diffusion Webui AI本地部署基本分为两种方式&#xff1a; 1、使用大佬的打包好的安装包一键部署 b站秋葉aaaki 2、手动部署&#xff08;个人实践记录&#xff09;参考文章 本地部署基本要求 1、 需要拥有NVIDIA显卡&#xff0c;GTX1060 &#xff08;或者同等算力的…

darknet 源码阅读

BBuf/Darknet: AlexeyAB-DarkNet源码解析 (github.com) 1. 依赖 1.1 环境要求 window系统或者linux系统。CMake版本高于3.8。CUDA 10.0&#xff0c;cuDNN>7.0。OpenCV版本高于2.4。Linux下需要GCC 或者Clang, Window下需要Visual Studio 15、17或19版。 1.2 数据集获取 …