简体   繁体   English

为什么python字典是空的?

[英]Why is python dictionary empty?

I have a function that runs through a list and then loads a dictionary. 我有一个通过列表运行的函数,然后加载一个字典。 It prints out fine within the function but the return dict is empty? 它在函数内输出正常,但返回字典是空的?

 95 def getAttachmentDict(messageIds):
 96     messageTable = defaultdict(list)
 97     for id in messageIds:
 98         uri = "/message/" + id
 99         res = getMessages(uri)
100         doc = xml.dom.minidom.parse(res)
101         idUrl = doc.getElementsByTagName("url")
102         alist = []
103         for i in idUrl:
104             urlData = i.childNodes[0].nodeValue
105             alist.append(urlData)
106         messageTable[id] = alist
107     print "\n"
108     for key, value in messageTable.items():
109         print "Message ID: " + key 
110         print "\n".join(value) 
111     return messageTable

169         getAttachmentDict(messageIds)
170         pdb.set_trace()
171         print messageTable

Message ID: MyUHRnzm7AXkFmiKocUjzn config-safety-report.html Message ID: RqfljNnyz63bBpGLtL4B2D individual-report.csv 消息ID:MyUHRnzm7AXkFmiKocUjzn config-safety-report.html消息ID:RqfljNnyz63bBpGLtL4B2D individual-report.csv

/Work/ftpTool/acquire.py(171)main() -> print messageTable (Pdb) n {} /Work/ftpTool/acquire.py(171)main()-> print messageTable(Pdb)n {}

You're not doing anything with the return value. 你没有对返回值做任何事情。 This is just printing some other variable you've called messageTable 这只是打印一些你称为messageTable变量

169         getAttachmentDict(messageIds)
170         pdb.set_trace()
171         print messageTable

try this instead 试试这个

169         messageTable = getAttachmentDict(messageIds)
170         pdb.set_trace()
171         print messageTable

When you call getAttachmentDict() , you need to assign the result to a name (which could be messageTable , or any other name you want to use). 当您调用getAttachmentDict() ,您需要将结果分配给名称(可以是messageTable ,或您要使用的任何其他名称)。 For example: 例如:

169         messageTable = getAttachmentDict(messageIds)
170         pdb.set_trace()
171         print messageTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM