我试图一次将多个词典保存到一个列表中,同时保持它们的值,但它不起作用
I am Trying to Save Multiple Dictionaries to a List at a Time Whilst Keeping Their Values, But It Isn't Working
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
为什么会这样,这是什么意思?有什么办法可以解决这个问题吗?
如@ggorlen 所说,使用名称作为键。
itemsInExistence = [{'name': 'Tester', 'stats': 1, 'rank': 1, 'amount': 1},
{'name': 'Mk II Death', 'stats': 10, 'rank': 5, 'amount': 3}]
def save_list2():
with open('all_items.txt', 'w') as f:
for item in itemsInExistence:
f.write('{name} {stats} {rank} {amount}\n'.format(**item))
本例需要 Python 3。
编辑:我已将 print(..., file=f)
更改为 f.write(...)
,现在它可以在 py2 和 py3 中使用。
EDIT2:一些解释。
with
语句关闭文件automatically.
列表 list
或 []
使用正整数索引(0
、1
等)
字典 dict
或 {}
使用键(在您的示例中 'name'
、'stats'
等)。参见 python docs。
for
语句按列表项或字典键迭代。你不需要ii
,item
是list item的内容,也就是dict。
for item in [1, 4, 'ala']:
print(item)
# prints:
# 1
# 4
# 'ala'
for key in {'anwer': 42, 'sto': 100, 1: 'first'}:
print(key)
# prints:
# 'answer'
# 'sto'
# 1
您可以通过 my_dict[key]
访问字典值或通过值 for value in my_dict.values
或通过键和值进行迭代:for key, value in my_dict.items()
.
我使用了关键字参数 **item
。在函数调用中 func(**{'a': 1, 'b': 2})
表示 func(a=1, b=2)
.
字符串格式 ''.format()
(或自 Python 3.6 起的格式字符串 f''
)允许使用 advanced syntax.
将数据直接放入字符串
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
为什么会这样,这是什么意思?有什么办法可以解决这个问题吗?
如@ggorlen 所说,使用名称作为键。
itemsInExistence = [{'name': 'Tester', 'stats': 1, 'rank': 1, 'amount': 1},
{'name': 'Mk II Death', 'stats': 10, 'rank': 5, 'amount': 3}]
def save_list2():
with open('all_items.txt', 'w') as f:
for item in itemsInExistence:
f.write('{name} {stats} {rank} {amount}\n'.format(**item))
本例需要 Python 3。
编辑:我已将 print(..., file=f)
更改为 f.write(...)
,现在它可以在 py2 和 py3 中使用。
EDIT2:一些解释。
with
语句关闭文件automatically.
列表 list
或 []
使用正整数索引(0
、1
等)
字典 dict
或 {}
使用键(在您的示例中 'name'
、'stats'
等)。参见 python docs。
for
语句按列表项或字典键迭代。你不需要ii
,item
是list item的内容,也就是dict。
for item in [1, 4, 'ala']:
print(item)
# prints:
# 1
# 4
# 'ala'
for key in {'anwer': 42, 'sto': 100, 1: 'first'}:
print(key)
# prints:
# 'answer'
# 'sto'
# 1
您可以通过 my_dict[key]
访问字典值或通过值 for value in my_dict.values
或通过键和值进行迭代:for key, value in my_dict.items()
.
我使用了关键字参数 **item
。在函数调用中 func(**{'a': 1, 'b': 2})
表示 func(a=1, b=2)
.
字符串格式 ''.format()
(或自 Python 3.6 起的格式字符串 f''
)允许使用 advanced syntax.