2017-08-25 17 views
1

遇到麻煩嘗試檢查一個字段是否在表中存在與否:檢查現場在一個context.table存在的python-表現

| category | info      | 
| Electrician | 74300 Paris 04 50 65 43 21 | 

比方說,我的表通常包含一個「名稱」字段,但在這種特定情況下它不包含。在這種情況下,如何檢查「名稱」字段的存在?

回答

0

您可以簡單地檢查您的列是否存在於表格的headings屬性中。當處理單個行的實例時,該屬性也存在,只是你知道的。

if 'name' in context.table.headings: 

    do_something() 

不過,我個人更喜歡處理表字典,使用這樣的事情

def make_dict_from_row(row): 
    """ 
    creates a dictionary of arguments (**kwargs) from a behave 
    table row. 
    """ 

    cells = [cell if cell != '' else None for cell in row.cells] 
    return dict(zip(row.headings, cells)) 

使用這個你可以簡單地這樣做

if 'name' in row: 

    do_something() 

進一步參考見table model