我正在尝试:
- 加载字典
- 更新/更改字典
- 节省
- (重复)
问题:我只想使用 1 个字典 (players_scores),但 defaultdict 表达式创建了一个完全独立的字典。如何加载、更新和保存到一本字典?
代码:
from collections import defaultdict#for manipulating dict
players_scores = defaultdict(dict)
import ast #module for removing string from dict once it's called back
a = {}
open_file = open("scores", "w")
open_file.write(str(a))
open_file.close()
open_file2 = open("scores")
open_file2.readlines()
open_file2.seek(0)
i = input("Enter new player's name: ").upper()
players_scores[i]['GOLF'] = 0
players_scores[i]['MON DEAL'] = 0
print()
scores_str = open_file2.read()
players_scores = ast.literal_eval(scores_str)
open_file2.close()
print(players_scores)