正则表达式检查已找到的组

问题描述 投票:0回答:1

我有以下代码:

regex_pattern = r'^(?P<value>[\.\d]+)(\s+)(?P<unit>[a-zA-z/%]+)$'
            for row in data:
                name, value = re.search(regex_pattern, str(row[1]))
                if value is not None and unit is not None:
                    print(value, unit)

我收到以下错误:

TypeError: cannot unpack non-iterable NoneType object 

我想是因为找不到任何匹配项,所以我可以检查结果是否为None。

但是如果找到name而不是value,反之亦然,怎么办?

python
1个回答
0
投票

当返回无时:

© www.soinside.com 2019 - 2024. All rights reserved.