简体   繁体   English

在openpyxl中读取超过10000行的.xlsx文件后没有值

[英]None values after reading more than 10000 rows of .xlsx file in openpyxl

I'm working on a application that can read out a large .xlsx (more than 20000 rows) with Openpyxl and can make some basic calculations with this data. 我正在开发一个可以使用Openpyxl读出大型.xlsx(超过20000行)的应用程序,并可以使用这些数据进行一些基本计算。 I try to read out 20000 rows in the example below. 我尝试在下面的示例中读出20000行。 When I run this code in the command promt of Windows the output is fine, but when I run it in a linux terminal or in the terminal of PyCharm the output after 10000 changes to None. 当我在Windows的命令promt中运行此代码时输出很好,但是当我在linux终端或PyCharm的终端中运行它时10000后的输出变为None。

I already tried to change the load_workbook read_only parameter to True, but that completely destroyed the performance of the application 我已经尝试将load_workbook read_only参数更改为True,但这完全破坏了应用程序的性能

from openpyxl import *

wb = load_workbook(filename="wind_data.xlsx", read_only=False)
ws = wb.active

output_data = [ws.cell(row=i, column=2).value for i in range(2, 20002)]

for i in range(0, len(output_data)):
    print(str(i+1) + " : " + str(output_data[i]))

The output I expect from this code would end with: 我期望从此代码输出的结果将以:

19998 : 6
19999 : 6
20000 : 6

But instead I get: 但相反,我得到:

19998 : None
19999 : None
20000 : None

I suggest using generator comprehension for really big collections. 我建议将生成器理解用于真正的大集合。

output_data = (ws.cell(row=i, column=2).value for i in range(2, 20002))

This might help in your case. 这可能对您的情况有所帮助。 I test it on my linux and it's works fine on excel with over 20k rows. 我在我的linux上测试它,它在超过20k行的excel上工作正常。 Secondly when you want to iterate thru such collection and also have indexes use function: 其次,当你想通过这样的集合进行迭代并且还有索引使用函数:

enumerate(collection) 枚举(集合)

Like: 喜欢:

for i, val in enumerate(output_data):
    print('{} : {}'.format(i+1, val)

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

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