python dic与str的相互转化
2017-04-25 23:48
253 查看
python dic与str的相互转化
我们可以比较容易的将字典(dict)类型转为字符串(string)类型。通过遍历dict中的所有元素就可以实现字典到字符串的转换:for key, value in sample_dic.items(): print "\"%s\":\"%s\"" % (key, value)
字符串(string)转为字典(dict)
如何将一个字符串(string)转为字典(dict)呢?其实也很简单,只要用 eval()或exec() 函数就可以实现了。>>> a = "{'a': 'hi', 'b': 'there'}" >>> b = eval(a) >>> b {'a': 'hi', 'b': 'there'} >>> exec ("c=" + a) >>> c {'a': 'hi', 'b': 'there'} >>>
原贴地址:http://blog.csdn.net/jenyzhang/article/details/49741283
相关文章推荐
- python dic与str的相互转化
- python中str(字符串)、list(列表)、tuple(元组)、dict(字典)相互转化关系及字典键-值遍历
- python字符串str和字节数组相互转化方法
- python字符串str和字节数组相互转化
- python unicode 和 str相互转化
- python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8')
- python字符串str和字节数组相互转化
- python基础---字符串类型str和字节类型bytes相互转化
- python字符串str和字节数组相互转化
- python 时间戳与时间相互转化
- python中将一个全部为int的list 转化为str的list
- python 下xml和dict相互转化,含attributes
- Python str和bytes的相互转换
- Python中list、tuple、str和dict之间的相互转换
- python中xlsx,csv以及json文件的相互转化
- python 字符数字相互转化
- numpy的array和python中自带的list之间相互转化
- python,php 日期时间和时间戳相互转化
- Python基础学习笔记3 - list 和 str 的区别,转化,list解析
- numpy.array和python中list的相互转化