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