python - 我如何将字典取出文件保存在 csv 文件中并将其存储为字典
问题描述
这是在我的 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)
解决方案
您可以使用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)
推荐阅读
- python - 训练时如何实现随机裁剪?
- linux - 在 linux 中,如何将特定的子目录压缩到名为父目录名称的自己的 zip 文件中,并将它们全部输出到单个目录中?
- python - 如何在python中不使用全局
- c++ - 如何动态更改 .wav 的速度或。实时使用 arduino 的 aiff 文件
- python - pandas.io.sql:“顶部”或附近的语法错误
- altair - Altair“找不到名为 'selectorXXX' 的选择”错误
- laravel - Laravel Command - Method Illuminate\Database\Eloquent\Collection::update does not exist
- c# - 为什么在使用 LINQ Sum 方法时出现异常“无法将类型 'bool' 隐式转换为 'long?'”以及如何解决?
- mysql - MySQL 5.7 作为文档存储和 _id 生成
- apache-flink - Flink 指标的 Prometheus 查询示例