Python 字典与字符串的互转实例

字典转换为字符串

if __name__ == '__main__':
  a = {'a' : 1, 'b' : 2, 'c' : 3}
  b = str(a)
  print(type(b))

输出结果为:

<class 'str'>

---------------------------------------------------------------

字符串转换为字典

if __name__ == '__main__':
  a = "{'a' : 1, 'b' : 2, 'c' : 3}"
  b = eval(a)
  print(type(b))

输出结果为:

<class 'dict'>

相关推荐