在openpyxl的优化阅读器中使用ws.iter_rows遍历一系列行 [英] iterating over a range of rows using ws.iter_rows in the optimised reader of openpyxl

查看:2519
本文介绍了在openpyxl的优化阅读器中使用ws.iter_rows遍历一系列行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读一个10x5324单元格的xlsx文件



这是我想要做的要点:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $

wb = load_workbook(filename)
ws = wb.get_sheet_by_name ('LOG')

col = {'Time':0 ...}

在ws.columns [col ['Time']] [1: ]:
print i.value.hour

代码运行时间太长那么它应该是(我在执行操作,而不是打印),一段时间后我不耐烦地取消它。



任何想法如何在优化的读者中工作?
我需要遍历一系列行,而不是遍历所有的行。
这是我试过的,但它是错误的:

  wb = load_workbook(filename,use_iterators = True)
ws = wb.get_sheet_by_name('LOG')
在ws.iter_rows [1:]中:
print i [col ['Time']] value.hour

有没有办法我没有范围功能?



<我想这样做的一个方法是:

 对于我在ws.iter_rows [1:]:
if i.row == startrow:
continue
print i [col ['Time']]。value.hour
if i.row == endrow:
break

但是有更优雅的解决方案吗?
(btw不起作用)

解决方案

下限的最简单的解决方案就是这样:

 #您的代码:
from openpyxl import load_workbook
filename ='file_path'
wb = load_workbook(filename,use_iterators = True)
ws = wb.get_sheet_by_name('LOG')

#解决方案1:
用于ws.iter_rows中的行(row_offset = 1 )
#每行执行一次代码

您可以使用 枚举 功能:

 #解决方案2:
start,stop = 1,100#这将允许您为枚举(ws.iter_rows())中的索引,行的下限和上限
设置:
如果start<指数停止:
#代码执行每行...

索引变量保持计数你是什​​么行,所以它可以用于代替范围或xrange。该方法非常简单,并且与范围或切片不同的迭代器一起使用,如果需要,也可以与下限一起使用。干杯!


I need to read an xlsx file of 10x5324 cells

This is the gist of what i was trying to do:

from openpyxl import load_workbook
filename = 'file_path'

wb = load_workbook(filename)
ws = wb.get_sheet_by_name('LOG')

col = {'Time':0 ...}

for i in ws.columns[col['Time']][1:]:
    print i.value.hour

The code was taking much too long to run then it should (I was performing operations, not printing) and after a while I got impatient and cancelled it.

Any idea how I can work it in the optimized reader? I need to iterate over a range of rows, not over all rows. This is what i tried, but it's wrong:

wb = load_workbook(filename, use_iterators = True)
ws = wb.get_sheet_by_name('LOG')
for i in ws.iter_rows[1:]:
    print i[col['Time']].value.hour

Is there any way I can do it without the range function?

I guess one way to do it would be:

for i in ws.iter_rows[1:]:
    if i.row == startrow:
        continue
    print i[col['Time']].value.hour
    if i.row == endrow:
        break

but is there a more elegant solution? (that doesn't work either btw)

解决方案

The simplest solution with a lower bound would be something like this:

# Your code:
from openpyxl import load_workbook
filename = 'file_path'
wb = load_workbook(filename, use_iterators=True)
ws = wb.get_sheet_by_name('LOG')

# Solution 1:
for row in ws.iter_rows(row_offset=1):
    # code to execute per row...

Here another way to execute what you describe, with the enumerate function:

# Solution 2:
start, stop = 1, 100    # This will allow you to set a lower and upper limit
for index, row in enumerate(ws.iter_rows()):
    if start < index < stop:
        # code to execute per row...

The index variable keeps count of what row you are on, so it can be used in place of range or xrange. This method is pretty straightforward and works with iterators unlike range or slicing, and can be used with just the lower bound too, if desired. Cheers!

这篇关于在openpyxl的优化阅读器中使用ws.iter_rows遍历一系列行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆