美文网首页
Python 字典与字符串的互转

Python 字典与字符串的互转

作者: w春风十里w | 来源:发表于2017-09-28 16:06 被阅读0次

字典转换为字符串

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'

相关文章

网友评论

      本文标题:Python 字典与字符串的互转

      本文链接:https://www.haomeiwen.com/subject/pdqvextx.html