问题描述
我正在尝试将python 2项目升级到python3。我正在读取文件并将其作为字典加载到内存中。在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)
解决方法
找到了解决此问题的方法。