python - 为什么即使满足 if 语句条件,我的 else 语句也会执行?
问题描述
我正在使用 for 循环在文本中搜索 for 循环内的值 usinf if else 语句。即使满足了我的搜索条件,我的 else 块也正在执行中。
这是我用来搜索值的文本:
SKU 产品描述 包装/尺寸 数量 数量 价格扩展 1 WL140.111 Clam Tuatua Medium NZ / 20-34 pcs / kg / 30.00 KG HK$109.25 HK$3,277.50 地点: KIT - Butchery (8%) 30.00 Edit Line Edit Alloc
这是我的代码:
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
解决方案
推荐阅读
- sql - 关于列问题的内部联接表
- python - Python Webbot:我怎样才能告诉他按下这个按钮?
- prolog - 转谓语;如何将反转元素的子列表附加到列表中?
- javascript - 嵌套数组映射
- php - 从 json 文件自动填充表
- javascript - 寻找有关 App Maker 的教育材料
- mysql - MySQL:选择连接的行,其中孩子不属于特定的父母 ID
- c# - 如何使用正则表达式从输入字符串中提取所有非字母数字字符?
- polymer - 聚合物构建后未找到 lit-element.js
- javascript - 为什么我的 JavaScript 数组在推送数据后是空的?