Python字典今天真的有我。我一直在倾吐堆栈,试图找到一种方法来对Python字典中的现有密钥做一个新值的简单追加,并且我在每次尝试时都会失败,并使用我在此处看到的相同语法。将多个值添加到Python字典中的单个键
这就是我要做的:
#cursor seach a xls file
definitionQuery_Dict = {}
for row in arcpy.SearchCursor(xls):
# set some source paths from strings in the xls file
dataSourcePath = str(row.getValue("workspace_path")) + "\\" + str(row.getValue("dataSource"))
dataSource = row.getValue("dataSource")
# add items to dictionary. The keys are the dayasource table and the values will be definition (SQL) queries. First test is to see if a defintion query exists in the row and if it does, we want to add the key,value pair to a dictionary.
if row.getValue("Definition_Query") <> None:
# if key already exists, then append a new value to the value list
if row.getValue("dataSource") in definitionQuery_Dict:
definitionQuery_Dict[row.getValue("dataSource")].append(row.getValue("Definition_Query"))
else:
# otherwise, add a new key, value pair
definitionQuery_Dict[row.getValue("dataSource")] = row.getValue("Definition_Query")
我得到一个属性错误:
AttributeError: 'unicode' object has no attribute 'append'
但我相信我做的一样的答案提供here
我尝试了各种其他方法,但没有与其他各种错误消息一起运行。我知道这可能很简单,也许我无法在网上找到正确的来源,但我被卡住了。任何人都在意帮忙吗?
谢谢, 迈克
呃...我知道我需要这样的东西。将值设置为列表。我想我只是看着太多的例子,然后我迷惑了自己。这正是我需要的。谢谢! – Mike