2013-05-07 221 views
0

運行代碼時ValueError異常:無效的基數爲10字面INT(): '結果'

data = urllib.urlencode(parameters) 
     req = urllib2.Request(url, data) 
       logging.info('shivaniHAHOHJSJKDNVJNCMBVJDCVHJSHHHHEKLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO') 
     logging.info(data) 
     try: 
      response = urllib2.urlopen(req) 
     except urllib2.URLError, e: 
      self.redirect('/error') 
     json_post = response.read() 
     data = json.loads(json_post) 
     self.redirect('/'+str(data)) 
     response_dict = simplejson.loads(json_post) 

     virustotal = VirusTotal() 
     logging.info('1111111111111111111111111111111111111111111111111') 
     logging.info('result') 
     logging.info(data[int('result')]) 
     if data ['result'] == 0: 
      virustotal_result = True 

我獲得以下在我的日誌文件

ValueError: invalid literal for int() with base 10: 'result' 
    INFO  2013-05-07 12:14:19,142 server.py:561] default: "GET /query?  url=abc.com&submit=Scan HTTP/1.1" 500 - 
    INFO  2013-05-07 12:16:01,857 server.py:561] default: "GET/HTTP/1.1" 200 1236 
    INFO  2013-05-07 12:16:01,894 server.py:561] default: "GET /images/logo.png HTTP/1.1" 304 - 
    INFO  2013-05-07 12:16:01,898 server.py:561] default: "GET /css/cssscript.css HTTP/1.1" 304 - 
    INFO  2013-05-07 12:16:01,943 server.py:561] default: "GET /images/left.png HTTP/1.1" 304 - 
    INFO  2013-05-07 12:16:01,950 server.py:561] default: "GET /images/right.png HTTP/1.1" 304 - 
    INFO  2013-05-07 12:16:01,950 server.py:561] default: "GET /images/bg.png HTTP/1.1" 304 - 
    INFO  2013-05-07 12:16:02,065 server.py:561] default: "GET /favicon.ico HTTP/1.1" 404 154 
    WARNING 2013-05-07 12:16:10,082 urlfetch_stub.py:453] Stripped prohibited headers from URLFetch request: ['Content-Length', 'Host'] 
    INFO  2013-05-07 19:16:23,915 main.py:420] shivaniHAHOHJSJKDNVJNCMBVJDCVHJSHHHHEKLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO 
    INFO  2013-05-07 19:16:23,917 main.py:421] resource=http%3A%2F%2Fgoogle.com&scan=1&key=b99eeaddac6ef5cf62746beffd71f1e16708b6db6085de243f050e241c6c671f 
    WARNING 2013-05-07 12:16:23,924 urlfetch_stub.py:453] Stripped prohibited headers from URLFetch request: ['Content-Length', 'Host'] 
    INFO  2013-05-07 19:16:33,023 main.py:432] 1111111111111111111111111111111111111111111111111 
    INFO  2013-05-07 19:16:33,026 main.py:433] result 
    ERROR 2013-05-07 19:16:33,028 webapp2.py:1528] invalid literal for int() with base 10: 'result' 


    Traceback (most recent call last): 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__ 
    return handler.dispatch() 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch 
    return method(*args, **kwargs) 
    File "C:\Users\Shivani\Desktop\malbee-phish (1)\main.py", line 434, in get 
    logging.info(data[int('result')]) 
ValueError: invalid literal for int() with base 10: 'result' 

什麼可以是可能的原因。 ??如果我嘗試數據[「結果」]不用INT()它給了我下面的錯誤 類型錯誤:列表索引必須是整數,而不是str的

回答

3

當然好了這條線將無法正常工作

logging.info(data[int('result')])如何int('result')應該工作?

你的堆棧跟蹤告訴你到底是什麼問題。

所以我問你的問題,你在哪裏的result

數據得到一個值「結果」]只有當數據是一本字典,你必須在它的值爲「結果」

一項重點工作

或者,如果數據是一個列表(這是TypError列表索引必須是整數,而不是str告訴你,那麼結果作爲一個變量必須在某個點賦予一個整數值。出現的結果(變量)曾經賦值。

+0

感謝輸入..但PLZ做看看此http://計算器.com/questions/16405750/typeerror-list-indices-must-be-integers-not-str-sometimes-working-sometimes- – katherine 2013-05-07 07:03:58

+0

這個人有同樣的問題。看看「數據」是什麼類型。這是非常基本的Python。現在看看原始的json。我敢打賭,它來自一些JavaScript,往返不會導致你所期望的。對json載入的內容進行一些基本的調試。最終,你不能擺脫你試圖將非整數字符串轉換爲int的事實。 - 這是行不通的,這是你問到的錯誤。 – 2013-05-07 07:14:33

+0

數據是列表類型..和IM分配值的結果在開始..結果= db.IntegerProperty()..我真的是新的python和結果的值打印是.. INFO 2013-05- 07 20:30:47,871 main.py:76] 可以給我一些語法指導,以解決同樣的問題。謝謝 – katherine 2013-05-07 07:28:21

2

您有int('result') whic h將永遠不會工作。也許你的意思是int(result)

(雖然我不能看到如何將工作要麼,看到你從來沒有分配給一個變量result。)

相關問題