Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理没有整数的变量_Python_String_Web Scraping - Fatal编程技术网

Python 如何处理没有整数的变量

Python 如何处理没有整数的变量,python,string,web-scraping,Python,String,Web Scraping,我正在从网站上删除一些数据 变量“scraped”可以是int,也可以是str 即“1块”或“无块” 当有int时,我提取它并执行一些代码 value = re.findall(r'\d+', scrapped) new_value = int(value[0]) ... 然而,当scraped只是一个没有数字的字符串时,我的程序就会中断 错误地 索引器:列表索引超出范围 当它只是一个str时,我如何忽略它,或者首先检查是否有int并继续您可以使用重新搜索该任务,方法如下: if re.sea

我正在从网站上删除一些数据
变量“scraped”可以是int,也可以是str
即“1块”或“无块”

当有int时,我提取它并执行一些代码

value = re.findall(r'\d+', scrapped)
new_value = int(value[0])
...
然而,当scraped只是一个没有数字的字符串时,我的程序就会中断 错误地

索引器:列表索引超出范围


当它只是一个str
时,我如何忽略它,或者首先检查是否有int并继续

您可以使用
重新搜索该任务,方法如下:

if re.search(r'\d+', scrapped):
    value = re.findall(r'\d+', scrapped)
    new_value = int(value[0])
    ...

请记住,在
re.search
re.findall
中,模式应该是相同的。我在脑子里把它复杂化了,现在我觉得很傻,我只是把它放在一个if语句中,然后它就工作了