我在尝试检查某个字段是否存在于表中时遇到问题:
| category | info |
| Electrician | 74300 Paris 04 50 65 43 21 |
假设我的表通常包含一个“名称”字段,但在这个特定情况下它不包含。在这种情况下,如何检查“名称”字段是否存在?
最佳答案
您可以简单地检查您的列是否出现在表格的 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以供进一步引用。
关于python - 检查字段是否存在于 python-behave 的 context.table 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45879363/