我如何取出字典文件并保存在csv文件中并将其存储为字典

时间:2020-08-05 16:56:44

标签: python dictionary file-io

这在我的csv文件中:

{'Name': 'The Hero', 'Min Damage': 2, 'Max Damage': 4, 'Defence': 1, 'HP': 20, 'Inventory': [], 'fought': False, 'Y': 1, 'X': 0}

这就是我试图带出字典的

def resumegame():
    filename = 'C:/test/savegame.csv'
    if not os.path.isfile(filename):
        return {}
    with open(filename) as ifh:
        return dict(line.split() for line in ifh)

1 个答案:

答案 0 :(得分:0)

您可以使用yaml模块

import yaml

def resumegame():
    filename = 'C:/test/savegame.csv'
    if not os.path.isfile(filename):
        return {}
    with open(filename) as ifh:
        return yaml.load(ifh)