ValueError: invalid literal for int() with base 10: ''
python编程中,有时会遇到以下错误,在此记录一下。错误信息ValueError: invalid literal for int() with base 10: ”具体报错信息如下:[2017-02-13 14:33:44,225] [ERROR] Internal Server Error: /data/notification/Traceback (most recent call la
python编程中,有时会遇到以下错误,在此记录一下。
错误信息
ValueError: invalid literal for int() with base 10: ”
具体报错信息如下:
[2017-02-13 14:33:44,225] [ERROR] Internal Server Error: /data/notification/
Traceback (most recent call last):
File “/data/python-virtualenv/cmdb-api/lib/python2.6/site-packages/django/core/handlers/base.py”, line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
……
File “/data/python-virtualenv/cmdb-api/lib/python2.6/site-packages/django/db/models/fields/init.py”, line 613, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ”
原因:
不要被一大堆信息吓蒙掉,从报错信息上看这是一个类型转换错误:
‘’无法转为int型。
命令行下做测试
C:\Users\lanyang>python
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> int('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>>
>>> int("x")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'x'
>>>
总结
归结一下,这类错误就是字符无法转换为int型。为了避免类似错误,以后进行int类型转换时,注意检查,或者直接加try 捕获下。
「智能机器人开发者大赛」官方平台,致力于为开发者和参赛选手提供赛事技术指导、行业标准解读及团队实战案例解析;聚焦智能机器人开发全栈技术闭环,助力开发者攻克技术瓶颈,促进软硬件集成、场景应用及商业化落地的深度研讨。 加入智能机器人开发者社区iRobot Developer,与全球极客并肩突破技术边界,定义机器人开发的未来范式!
更多推荐
所有评论(0)