我正在使用正則表達式去除數據。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