如何在Python中使用Openpyxl查找单个列/行上的有效元素数?

时间:2019-10-24 23:56:33

标签: python python-2.7 openpyxl

我正在使用openpyxl。

  1. 我有下面的基本电子表格。

enter image description here

  1. 我试图使用len()和filter来获取特定列中的有效元素数量,但仍然没有得到我想要的。

示例代码:

load_xls_file = open("./sample.xlsx", "r") 
wb = load_workbook(load_xls_file)
sheet = wb.get_sheet_by_name("Sheet")


rock = len(sheet['A'])

print '_code : Value of rock from spreadsheet is',rock 
print '_code : Values are', filter(None,sheet['A'])
print '_code : Values are', sheet['A'] 
print '_code : Values of b', len(sheet['B']) 

输出:

    _code : Value of rock from spreadsheet is 30
    _code : Values are (<Cell u'Sheet'.A1>, <Cell u'Sheet'.A2>, <Cell u'Sheet'.A3>, <Cell u'Sheet'.A4>, <Cell u'Sheet'.A5>, <Cell u'Sheet'.A6>, <Cell u'Sheet'.A7>, <Cell u'Sheet'.A8>, <Cell u'Sheet'.A9>, <Cell u'Sheet'.A10>, <Cell u'Sheet'.A11>, <Cell u'Sheet'.A12>, <Cell u'Sheet'.A13>, <Cell u'Sheet'.A14>, <Cell u'Sheet'.A15>, <Cell u'Sheet'.A16>, <Cell u'Sheet'.A17>, <Cell u'Sheet'.A18>, <Cell u'Sheet'.A19>, <Cell u'Sheet'.A20>, <Cell u'Sheet'.A21>, <Cell u'Sheet'.A22>, <Cell u'Sheet'.A23>, <Cell u'Sheet'.A24>, <Cell u'Sheet'.A25>, <Cell u'Sheet'.A26>, <Cell u'Sheet'.A27>, <Cell u'Sheet'.A28>, <Cell u'Sheet'.A29>, <Cell u'Sheet'.A30>)
    _code : Values are (<Cell u'Sheet'.A1>, <Cell u'Sheet'.A2>, <Cell u'Sheet'.A3>, <Cell u'Sheet'.A4>, <Cell u'Sheet'.A5>, <Cell u'Sheet'.A6>, <Cell u'Sheet'.A7>, <Cell u'Sheet'.A8>, <Cell u'Sheet'.A9>, <Cell u'Sheet'.A10>, <Cell u'Sheet'.A11>, <Cell u'Sheet'.A12>, <Cell u'Sheet'.A13>, <Cell u'Sheet'.A14>, <Cell u'Sheet'.A15>, <Cell u'Sheet'.A16>, <Cell u'Sheet'.A17>, <Cell u'Sheet'.A18>, <Cell u'Sheet'.A19>, <Cell u'Sheet'.A20>, <Cell u'Sheet'.A21>, <Cell u'Sheet'.A22>, <Cell u'Sheet'.A23>, <Cell u'Sheet'.A24>, <Cell u'Sheet'.A25>, <Cell u'Sheet'.A26>, <Cell u'Sheet'.A27>, <Cell u'Sheet'.A28>, <Cell u'Sheet'.A29>, <Cell u'Sheet'.A30>)
_code : Values of b 30

len()和filter都没有提供期望值,即7而不是一直输出最大值30。即使我这样做,len(sheet ['B'])仍然提供相同的值30。

我犯了任何简单的错误吗?请提供您的评论。

2 个答案:

答案 0 :(得分:0)

电子表格是行和列的表,因此,如果电子表格具有30行,则所有列均具有30个单元格。至于某个单元是否处于活动状态,您必须决定执行此操作的标准,但这很简单:

not_empty = [c for c in ws.iter_rows(min_col=1, max_col=1, values_only=True) if c[0] is not None]
print(len(not_empty))

答案 1 :(得分:0)

  

问题:获取特定列中的有效元素数


  

工作表

 var azureServiceTokenProvider = new AzureServiceTokenProvider();
 string accessToken =  azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com");
Title   Title   Title   
1       3       4   
None    None    None    
1       3       4   

  

OOP解决方案

使用min_col = 1 # 'A' val_counter = 0 # Iterate all Rows, starting at 'min_row=' # Iterate only ONE Column, therefore 'min_col=' and 'max_col=' have the same value # Returns a Tuple of Column Values ((value A2,), (value A3), ...) for cells in ws.iter_rows(min_row=2, min_col=min_col, max_col=min_col, values_only=True): value = cells[0] # Condition, which to count if value is not None: val_counter += 1 print('Values are {}'.format(val_counter)) # >>> Values are 2 方法扩展openpyxl class Worksheet

.filter(...
  

用法

import openpyxl

class Worksheet:
    def __init__(self, pyxl):
        for attr in ['filter', 'filter_list']:
            setattr(pyxl.worksheet.worksheet.Worksheet, 
                    attr, 
                    getattr(Worksheet, attr)
                   )

    def filter(self, f, range=None, values_only=True):
        cells = self.iter_rows(min_row=range[0],
                               min_col=range[1],
                               max_row=range[2],
                               max_col=range[3],
                               values_only=values_only
                              )

        for row in cells: 
            yield from (c for c in row if f(c))

    def filter_list(self, f, range=None, values_only=True):
        return [v for v in self.filter(f, range, values_only)]

# Extend openpyxl Worksheet
Worksheet(openpyxl)