Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python openpyxl遍历列的单元格=>;类型错误:';发电机&x27;对象不可下标_Python_Excel_Loops_Openpyxl - Fatal编程技术网

Python openpyxl遍历列的单元格=>;类型错误:';发电机&x27;对象不可下标

Python openpyxl遍历列的单元格=>;类型错误:';发电机&x27;对象不可下标,python,excel,loops,openpyxl,Python,Excel,Loops,Openpyxl,我想循环一列中的所有值,以将它们作为dict中的键进行保护。据我所知,一切正常,但python不同意 所以我的问题是:“我做错了什么?” 因此,虽然它可以解决我的问题,但几行之后,在我的代码中,它会返回给我。我将在代码中使用您的建议来提取密钥 我的主要问题是: 为什么它不起作用,我相信我以前用过这个代码段,这也是谷歌搜索时人们建议这样做的方式 ========================================================================== &g

我想循环一列中的所有值,以将它们作为dict中的键进行保护。据我所知,一切正常,但python不同意

所以我的问题是:“我做错了什么?”

因此,虽然它可以解决我的问题,但几行之后,在我的代码中,它会返回给我。我将在代码中使用您的建议来提取密钥

我的主要问题是:

为什么它不起作用,我相信我以前用过这个代码段,这也是谷歌搜索时人们建议这样做的方式

==========================================================================

>>> for column in ws.iter_cols(min_col = 2):
...     for cell in column:
...             print(cell.value)

检查图纸的所有列,第一列除外。现在,我仍然需要排除前3行。columns返回一个列生成器,因为这在大型工作簿上更有效,就像您的情况:您只需要一列。现在提供了直接获取列的选项:
ws['A']

显然,openpyxl模块在我不知情的情况下升级到了2.4

>>> for col in ws.iter_cols(min_col = 2, min_row=4):
...     for cell in col:
...         print(cell.value)
我们应该做到这一点。 我发布了这个帖子,以防其他人也在寻找相同的答案

iter_cols(min_col=None, max_col=None, min_row=None, max_row=None)[source]

Returns all cells in the worksheet from the first row as columns.

If no boundaries are passed in the cells will start at A1.

If no cells are in the worksheet an empty tuple will be returned.
Parameters: 

    min_col (int) – smallest column index (1-based index)
    min_row (int) – smallest row index (1-based index)
    max_col (int) – largest column index (1-based index)
    max_row (int) – smallest row index (1-based index)

Return type:    

generator

我是个新手,但我基于openpyxl文档提出了以下代码,用于打印列的单元格内容:

x = sheet.max_row + 1

for r in range(1,x):
    d=sheet.cell(row=r,column=2)
    print(d.value)

如果您想使用旧的数字表示法,可以使用“openpyxl.utils”中的“get_column_letter”。
iter_cols(min_col=None, max_col=None, min_row=None, max_row=None)[source]

Returns all cells in the worksheet from the first row as columns.

If no boundaries are passed in the cells will start at A1.

If no cells are in the worksheet an empty tuple will be returned.
Parameters: 

    min_col (int) – smallest column index (1-based index)
    min_row (int) – smallest row index (1-based index)
    max_col (int) – largest column index (1-based index)
    max_row (int) – smallest row index (1-based index)

Return type:    

generator
x = sheet.max_row + 1

for r in range(1,x):
    d=sheet.cell(row=r,column=2)
    print(d.value)