我正在使用正则表达式去除数据。AttributeError:'NoneType'对象没有属性'组'尝试循环
如果我对数据进行硬编码并将其与正则表达式进行匹配,它可以正常工作。但是,如果使用一个For Each循环,循环变量传递给re.match()
,我得到以下错误:
re.VERBOSE
AttributeError: 'NoneType' object has no attribute 'groups'**
我的代码:
trs = soup.findAll("tr")
for tr in trs:
c = unicodedata.normalize('NFKD', tr.text)
y.append(str(c))
for x in y:
#data1 = "Ambala 1.2 Onion 1200 2000 1500"
x1 = ([c.strip() for c in re.match(r"""
(?P<market>[^0-9]+)
(?P<arrivals>[^ ]+)
(?P<variety>[^0-9]+)
(?P<min>[0-9]+)
\ (?P<max>[0-9]+)
\ (?P<modal>[0-9]+)""",
x,
re.VERBOSE
).groups()])
如果我设置data1 = "Ambala 1.2 Onion 1200 2000 1500"
,然后正常工作。
谁能告诉我如何在循环中正确地迭代它以获取值并避免错误。
你能指定你想要做什么吗?为什么你要循环每个字符? – Mariano