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
我尝试过其他各种方法,但没有成功地处理其他各种错误消息。我知道这可能很简单,也许我在网上找不到合适的来源,但我被卡住了。有人愿意帮忙吗?在
谢谢,
迈克