我正在努力制作syre,但数字值和小数点都留在了。
字符串
[\n\t\t€249.99\xa0\n\t\t\t]
代码
str(re.compile("^[0-9]\d*(\.\d+)?$", PRICE[0]))
错误
Traceback (most recent call last):
File "getPrice.py", line 59, in <module>
JSON_FILE.write("{\"price\":\"" + str(re.compile("^[0-9]\d*(\.\d+)?$", PRICE[0])) + "\"},")
File "C:\...\Python\Python36-32\lib\re.py", line 233, in compile
return _compile(pattern, flags)
File "C:\...\Python\Python36-32\lib\re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\...\Python\Python36-32\lib\sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "C:\...\Python\Python36-32\lib\sre_parse.py", line 856, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
TypeError: unsupported operand type(s) for &: 'lxml.etree._ElementUnicodeResult' and 'int'
答案 0 :(得分:2)
您错误地使用了re.compile
。以下是使用re.search
的解决方案:
s = '\n\t\t€249.99\xa0\n\t\t\t'
re.search('[0-9.]+', s).group() # Returns '249.99'
您还可以使用re.findall
,re.match
或re.sub
获得相同的结果。