![](/img/trans.png)
[英]Is there a way to check which check boxes have been checked in a PyQt5 table
[英]Regex check which groups have been found
我有以下代码:
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
,反之亦然,我该如何检查?
当这返回 None 时:
名称,值 = re.search(regex_pattern, str(row[1]))
您无法将其解压缩为名称和值。 将您的代码重新编写为:
search = re.search(regex_pattern, str(row[1]))
if search is not None:
name, value = search
第三,我不知道__unit__
在这里做什么。 你是想写名字吗?
或者,您可以让正则表达式可选以返回名称或值或两者。 目前,它必须返回两者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.