我正在使用for循环在for循环内的else语句中搜索文本以获取值usinf。即使我的搜索条件已满足,我的else块也正在执行。
这是我用来搜索值的文本:
SKU产品描述。包装/大小QtyUOM价格扩展 1 WL140.111 Clam Tuatua Medium NZ / 20-34个/千克/ 30.00 KG HK $ 109.25 HK $ 3,277.50 位置:屠宰场(KIT-屠杀(8%)30.00)
这是我的代码:
whole_details = re.compile(r'Item([\$\w\s\.\/\-\,:()%]+)(?:Sub Total)')
wd = whole_details.search(text)
wd_text = wd.group(1)
products = ["Yoghurt Passionfruit Organic", "Yoghurt Plain Organic Vegan", "Clam Tuatua Medium 20-", "Clam Tuatua Medium NZ /", "Oyster Pacific NZ /"]
for product in products:
if wd_text.find(product) != -1:
re_qty = re.compile(rf'{product}\s([\d.]+)')
qty_search = re_qty.search(wd_text)
qty = qty_search.group(1)
print("Product Description : " + product)
print("Quantity : " + qty)
else:
print("No product")
我现在得到的输出是
Product Description : Clam Tuatua Medium NZ /
Quantity : 20
No products