- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Excel 电子表格,我需要每天将其导入 SQL Server。该电子表格将包含大约 50 列的大约 250,000 行。我已经使用几乎相同的代码使用 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 个不同的脚本中维护相似的代码是没有意义的。
关于如何进行的任何建议?
最佳答案
你可以 iterate在工作表上:
def UseOpenpyxl(file_name):
wb = openpyxl.load_workbook(file_name, read_only=True)
sheet = wb.active
rows = sheet.rows
first_row = [cell.value for cell in next(rows)]
data = []
for row in rows:
record = {}
for key, cell in zip(first_row, row):
if cell.data_type == 's':
record[key] = cell.value.strip()
else:
record[key] = cell.value
data.append(record)
return data
这应该可以扩展到大文件。如果列表,您可能希望对结果进行分 block data
变得太大了。
现在 openpyxl 版本花费的时间大约是 xlrd 版本的两倍:
%timeit xlrd_results = UseXlrd('foo.xlsx')
1 loops, best of 3: 3.38 s per loop
%timeit openpyxl_results = UseOpenpyxl('foo.xlsx')
1 loops, best of 3: 6.87 s per loop
请注意,xlrd 和 openpyxl 对整数和 float 的解释可能略有不同。对于我的测试数据,我需要添加 float()
以使输出具有可比性:
def UseOpenpyxl(file_name):
wb = openpyxl.load_workbook(file_name, read_only=True)
sheet = wb.active
rows = sheet.rows
first_row = [float(cell.value) for cell in next(rows)]
data = []
for row in rows:
record = {}
for key, cell in zip(first_row, row):
if cell.data_type == 's':
record[key] = cell.value.strip()
else:
record[key] = float(cell.value)
data.append(record)
return data
现在,两个版本对我的测试数据给出了相同的结果:
>>> xlrd_results == openpyxl_results
True
关于python - 与 xlrd 相比,使用 openpyxl 读取 Excel 文件的速度要慢得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35823835/
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Haskell: difference between . (dot) and $ (dollar sign) 好
我对 Java 平台没有任何了解,我想知道可以使用哪些工具(和方法)来帮助开发用 Java 编写的可维护代码。 我知道可以使用: 适用于任何环境的敏捷方法 用于单元测试代码的 jUnit/jMock(
我们的产品需要支持 IE9,但我们一直假设 IE9 支持 IE10+ CSS 规则。 是否有一种巧妙的方法来获取在 IE10+ 中有效但在 IE9 中不受支持的所有 CSS 规则,目的是在静态代码分析
我需要为 MyString 类重载运算符 + 和 +=。 MyString.h class MyString { char* m_pStr; }; 主要
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在升级现有的旧代码以使用 VS 2019*,在代码中我有以下函数在返回行失败: int foo(const char *fn) const { ofstream out(fn,ios::b
我想使用 R2D3 pacakge 在 R 中,但我不确定这个包与 D3.js 库的关系。 R2D3 是否以任何方式限制 D3 的功能?我们可以将 R 中的所有 D3 功能和特性与 R2D3 一起使用
我正在使用 WPF 语音识别库,试图在桌面应用程序中使用它来替代菜单命令。 (我想专注于没有键盘的平板电脑体验)。它可以工作 - 有点,除了识别的准确性太差以至于无法使用。所以我试着听写到 Word。
我在学校参加数据库类(class)。老师给了我们一个简单的练习:考虑以下简单的模式: Table Book: Column title (primary key) Column gen
我正在尝试学习 MVVM 模式,特别是当 View 表示数据库表时该怎么做,但 View 有几个元素表示单个数据库字段。举个简单的例子: 假设我有一个 DateTime 类型的数据库字段(每个数据库字
我有两张 table 。表单有约 77000 行。日志约有 270 万行。 以下查询将在不到一秒的时间内返回“30198”: SELECT COUNT(DISTINCT logs.DOCID) FRO
当您在 Eviews 中进行回归时,您会得到一组这样的统计数据: 在 R 中有没有一种方法可以在一个列表中获得所有/大部分关于 R 回归的统计数据? 最佳答案 请参阅summary,它将为大多数回归对
如果我枚举 type XType int const ( X1 XType = iota X2 ... Xn ) var XTypeNames = []string{"x1", "x2
我正在试用 ranger R包加速做了很多randomForest计算。我正在检查我从中得到的预测,并注意到一些有趣的事情,因为所做的预测完全不正确。 以下是比较 randomForest 的可重现示
我发现 Clang 编译速度比 GCC 慢了四倍。知道是什么原因造成的吗? ebg@tsuki(250)$ time /usr/bin/cc -DHC4 -DSAFETY -DNOREDUCE -DN
我注意到在尝试以 JSON 格式发布表单数据时,以下内容不起作用: $.ajax({ type: "POST", url: url, data: JSON.string
我的代码库中有很多 #if DEBUG/#endif 语句,它们大多具有断言类型逻辑,我不敢在生产环境中运行这些逻辑。 [Conditional("DEBUG")] public void Check
所以我正在开发一个平方根计算器,但我不知道 while 循环是否比 do while 循环更适合。 double x, y = 1.0, newY, squareRoot; bool
我有两个列表,一个是所有语言,另一个是网站拥有的语言子集,我的想法是返回所有语言,但如果子集的元素对应于所有语言的列表,则更改 bool 值的属性. 语言的DTO: public class DTOL
以下控制台应用程序运行正常 - 我很惊讶它没有出错。 class DelegateExperiments { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
我是一名优秀的程序员,十分优秀!