Python ValueError: invalid literal for int() with base 10 实用解决方法
投稿:junjie
这篇文章主要介绍了Python ValueError: invalid literal for int() with base 10 实用解决方法,本文使用了一个取巧方法解决了这个问题,需要的朋友可以参考下
今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了
复制代码 代码如下:
totalCount = '100' totalPage = int(totalCount)/20
ValueError: invalid literal for int() with base 10的错误
网上同样的错误有人建议用round(float(“1.0″)),但是解决不了我这个问题,round(float(“1.0″))是用于解决浮点数转换为整形数的,
而我这个则是因为原字符串转换为整形后做除法,虽然一段时间内可能不报错,但时间久了就会提示(其实就是一个warning,但是会强制终止你的程序),正确解决方法如下:
复制代码 代码如下:
去除掉字符串中的非数字字符即可。亲测可用
您可能感兴趣的文章:
- Python异常 ValueError的问题
- 解决Python报错:ValueError:operands could not be broadcast together with shapes
- 解决Python报错Valueerror: Expected 2d Array Got 1d Array Instead
- Python中ValueError报错的原因和解决办法
- Python报错ValueError: cannot reindex from a duplicate axis的解决方法
- Python报错ValueError: cannot convert float NaN to integer的解决方法
- 解决Python报错ValueError list.remove(x) x not in list问题
- Python中异常类型ValueError使用方法与场景
- Python ValueError: all input arrays must have the same shap的问题解决