1
由於某些原因,它不讓我將字符串項添加到字典中的某個鍵。將字典附加到主列表
這裏是我的代碼:
例如splitUserList(line)
:
>>> splitUserList("1|24|M|technician|85711 ")
['1', 24, 'M', 'technician', '85711']
代碼:
def createUserList():
userList = []
totalUserList = []
f = open("u.user.txt")
for line in f:
userDict = {}
singleUserList = splitUserList(line)
userDict["age"] = singleUserList[1]
userDict["gender"] = singleUserList[2]
userDict["occupation"] = singleUserList[3]
singleUserList["zip"] = singleUserList[4]
userList.append(userDict)
return userList
爲什麼不工作?
Traceback (most recent call last):
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 20, in createUserList
TypeError: list indices must be integers, not str
感謝@ C.B。討厭那樣的錯誤 – user3491255
@ user3491255接受他的回答然後請? - ) –