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: ”
不要被一大堆信息嚇蒙掉,從報錯信息上看這是一個類型轉(zhuǎn)換錯誤: ‘’無法轉(zhuǎn)為int型。
命令行下做測試
C:/Users/lanyang>pythonPython 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32Type "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'>>>歸結(jié)一下,這類錯誤就是字符無法轉(zhuǎn)換為int型。為了避免類似錯誤,以后進(jìn)行int類型轉(zhuǎn)換時,注意檢查,或者直接加try 捕獲下。
新聞熱點(diǎn)
疑難解答