简体   繁体   English

Openpyxl Python-如何遍历大文件并在特定列中返回值

[英]Openpyxl Python - How to iterate through large file and return values in specific columns

I am quite new to programming and am using openpyxl to return some values from a spreadsheet. 我是编程新手,正在使用openpyxl从电子表格返回一些值。

The spreadsheet is large and contains about 90,000 rows and 50 columns. 电子表格很大,包含大约90,000行和50列。

I've created a simplified version of the problem in the image below with just a few rows: 我在下图中创建了问题的简化版本,仅包含几行:
Excel Screenshot Here Excel屏幕截图在这里
What I am trying to do is iterate through all the rows in the spreadsheet and where column B = "Y", take the corresponding value of the cell in the column "code" and perform a function with that code. 我想要做的是遍历电子表格中的所有行,并且其中B列=“ Y”,取“ code”列中单元格的相应值,并使用该代码执行功能。

I've been trying to use sheet.iter_rows but not had much luck and have had some really slow performance. 我一直在尝试使用sheet.iter_rows,但是运气不佳,并且性能确实很慢。

for y_row in sheet.iter_rows(min_row=1, max_row=4, min_col=2, max_col=4):
        for cell in row:
            if cell.value == "Y"

How do I get cell.value to refer specifically to column 2? 如何获取cell.value专门引用第2列?

Any help would be much appreciated. 任何帮助将非常感激。

If you don't need to edit the file then you can take advantage of read-only mode. 如果您不需要编辑文件,则可以利用只读模式。 This will load the file much faster. 这样可以更快地加载文件。

wb = load_workbook(filename_and_path, read_only=True)
ws = wb.active
# Only look at cells in column B
for row in ws.iter_rows(min_col=2, max_col=2):
    if row[0].value:
        # code body.

Try: 尝试:

ckecked = 2
code = 4

for y_row in sheet.iter_rows(min_row=1, max_row=4, min_col=2, max_col=4):
    if y_row[checked-1].value == "Y":
        myfunc( y_row[code-1].value )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM