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 "" , line 1, in
ValueError: invalid literal for int() with base 10: ''
>>>
>>> int("x")
Traceback (most recent call last):
File "" , line 1, in
ValueError: invalid literal for int() with base 10: 'x'
>>>
归结一下,这类错误就是字符无法转换为int型。为了避免类似错误,以后进行int类型转换时,注意检查,或者直接加try 捕获下。