我正在尝试将 python 2 项目升级到 python 3。我正在读取一个文件并将其作为字典加载到内存中。在 python3 中,我注意到键是字节类型,而在 python 2 中它是文本。
with open(LOCAL_CACHE_FILE) as local_cache:
metrics_data = yaml.load(local_cache)
return metrics_data
该地图在 python 3 中如下所示
{b'max_write_delay': 44, b'score': 99.9980299448383, b'updated_at': 1598915750, b'write_delay': 44, b'min_write_delay': 4}
输入文件看起来像
CLUSTER.A:
max_write_delay: 71
min_write_delay: 50
score: 100.0
updated_at: 1596174141
write_delay: 71
CLUSTER.B:
max_write_delay: 71
min_write_delay: 50
score: 100.0
updated_at: 1596174141
write_delay: 71
该文件是按以下方式编写的。
with open(tmp_file_name, 'w') as tmp_file:
yaml.dump(metrics_data, tmp_file)