-6ren">
gpt4 book ai didi

python - 在 Python 中使用带正则表达式的 OR 语句时防止列表中出现空元素

转载 作者:行者123 更新时间:2023-11-28 19:24:05 25 4
gpt4 key购买 nike

我正在使用正则表达式从一个网站编译价格。

PriceFinder = re.compile('(?<=\n\s\<b>\$)(\d*\.\d{2})(?=\<\/)|(?<=\"FF0000"><b>\$)(\d*\.\d{2})(?=\<\/)')
Price = re.findall(PriceFinder, str(soup))
print Price

我得到以下结果:

[('', '30.99'), ('', '30.99'), ('', '30.99'), ('34.99', ''), ('34.99', '')

我想知道我必须将什么添加到我的正则表达式中才能获得没有任何空元素的列表。

['30.99','30.99','30.99','34.99','34.99']

谢谢

最佳答案

好的,我已经编写了我的第一个 python 来回答这个问题:

#!/usr/bin/python

import re

r = re.compile('(?:\n\s\<b>\$|\"FF0000"><b>\$)(\d*\.\d{2})(?=\<\/)')
p = re.findall(r, '<x y="FF0000"><b>$30.99</b></x>\n <b>$31.99</b>')
print p

打印出 ['30.99', '31.99']

关于python - 在 Python 中使用带正则表达式的 OR 语句时防止列表中出现空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com