python将字典转换为字符串,再转为字典

import ast

字典 info = {‘name’ : ‘Lee’, ‘gender’ : ‘male’, ‘age’: 18}
字符串 info_str = str(info)
则info_str = ‘{‘name’ : ‘Lee’, ‘gender’ : ‘male’, ‘age’: 18}’
dic = ast.literal_eval(info_str)
则dic = {‘name’ : ‘Lee’, ‘gender’ : ‘male’, ‘age’: 18}

你可能感兴趣的:(python)