2014-11-22 93 views
0

我有一個問題測試一個簡單的Python其餘的調用 當我運行測試時,我總是得到這個錯誤 在代碼中的東西也許我正在訪問一個空對象但是在哪裏 有什麼想法?謝謝 !WebPy'NoneType'對象沒有屬性'__getitem__'

#!/usr/bin/env python 
import web 
import xml.etree.ElementTree as ET 

tree = ET.parse('user.xml') 
root = tree.getroot() 

urls = (
    '/users', 'list_users', 
    '/users/(.*)', 'get_user' 
) 

app = web.application(urls, globals()) 

class list_users: 
    def GET(self): 
     output = 'users:['; 
     for child in root: 
      print 'child', child.tag, child.attrib 
      output += str(child.attrib) + ',' 
     output += ']'; 
     return output 

class get_user: 
    def GET(self, user): 
     for child in root: 
      if child.attrib['id'] == user: 
       return str(child.attrib) 

if __name__ == '__main__': 
    app.run() 
+3

你需要給我們**完整的**回溯。 – 2014-11-22 21:45:08

回答

0

您是否創建了user.xml文件並將其保存在與此Python文件相同的目錄中?

我複製你的代碼並運行它,它的工作原理。所以這個問題可能是在安裝web.py。

只是刪除對webpy和做相關的所有文件夾:

1)下載web.py - 從here的文件夾。

2)從此,將'web'文件夾保存到當前目錄。

3)現在運行你的程序,它應該工作。

相關問題