【发布时间】:2023-04-04 17:24:01
【问题描述】:
我正在使用 Python 3.7。我很难从字符串的开头提取数字。字符串是从 HTML 元素派生的,就像这样
elt.text
'3 reviews'
但是,当我尝试在此处使用逻辑获取数字时 -- Extract Number from String in Python ,我收到以下错误
int(filter(str.isdigit, elt.text))
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'filter'
有没有更好的方法从字符串的开头获取数字?
【问题讨论】:
-
filter返回一个迭代器,而不仅仅是您要查找的数字。您不能将filter转换为int。错误是说int() argument must not be a 'filter' object
标签: python python-3.x string numbers