我试着让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'发布于 2017-04-27 07:48:06
您使用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实现相同的结果。
https://stackoverflow.com/questions/43651480
复制相似问题