检查 python-beact 中的 context.table 中是否存在字段



我在尝试检查表中是否存在字段时遇到问题:

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

假设我的表通常包含一个"name"字段,但在这种特定情况下它没有。在这种情况下,如何检查"名称"字段是否存在?

您可以简单地检查您的列是否存在于表的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()

有关进一步参考,请参阅表模型。

最新更新