【发布时间】:2020-03-12 15:36:59
【问题描述】:
itemsInExistence = [{'name': 'Tester', 'stats': 1, 'rank': 1, 'amount': 1}, {'name': 'Mk II Death', 'stats': 10, 'rank': 5, 'amount': 3}]
def save_list2():
f = open('all_items.txt', 'w')
ii = 0
for item in itemsInExistence:
print(f.write(itemsInExistence[ii][0], ''))
f.write(itemsInExistence[ii][1] + ' ')
f.write(itemsInExistence[ii][2] + ' ')
f.write(itemsInExistence[ii][3] + '\n')
ii += 1
它说 f.write(itemsInExistence[ii][0], '') 它给了我错误
密钥错误:0
为什么会这样?这是什么意思?有没有办法解决这个问题?
【问题讨论】:
-
使用
item["name"]、item["stats"]等。您的字典中没有 0、1、2 等的键。 -
在代码中,第一行已经产生了语法错误。最好将您的代码复制并粘贴到问题中。
标签: python dictionary save