Home  >  Article  >  Backend Development  >  Python ValueError: invalid literal for int() with base 10 实用解决方法

Python ValueError: invalid literal for int() with base 10 实用解决方法

WBOY
WBOYOriginal
2016-06-10 15:10:214081browse

今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了

复制代码 代码如下:

totalCount = '100' totalPage = int(totalCount)/20
ValueError: invalid literal for int() with base 10的错误

网上同样的错误有人建议用round(float(“1.0″)),但是解决不了我这个问题,round(float(“1.0″))是用于解决浮点数转换为整形数的,
而我这个则是因为原字符串转换为整形后做除法,虽然一段时间内可能不报错,但时间久了就会提示(其实就是一个warning,但是会强制终止你的程序),正确解决方法如下:

复制代码 代码如下:

去除掉字符串中的非数字字符即可。亲测可用
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn