字典(dict)转为字符串(string)

我们可以比较容易的将字典(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'}
>>>

python string和dict转换的更多相关文章

  1. python基础-2 编码转换 pycharm 配置 运算符 基本数据类型int str list tupple dict for循环 enumerate序列方法 range和xrange

    1.编码转换 unicode 可以编译成 UTF-U GBK 即 #!/usr/bin/env python # -*- coding:utf-8 -*- a = '测试字符' #默认是utf-8 a ...

  2. python基础之dict、set及字符

    python基础之dict.set及字符串处理 本节内容 字典介绍及内置方法 集合介绍 字符串处理 1.字典介绍及内置方法 字典是python中唯一的映射类型,采用键值对(key-value)的形式存 ...

  3. python string module

    String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...

  4. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  5. 第四篇:python基础之dict、set及字符

    python基础之dict.set及字符   python基础之dict.set及字符串处理 本节内容 字典介绍及内置方法 集合介绍 字符串处理 1.字典介绍及内置方法 字典是python中唯一的映射 ...

  6. python任意进制转换

    python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...

  7. Python string interning原理

    原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ...

  8. python实现进制转换(二、八、十六进制;十进制)

    python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...

  9. python中json对象转换出错解决方法

    今天在使用python中的json转换碰到一个问题: 接收一个post的json字符串: s={"username":"admin","passwor ...

随机推荐

  1. ProxySQL 监控和统计

    ProxySQL 监控和统计 很多有价值的统计数据在stats和monitor库中. admin@127.0.0.1 [(none)]>SHOW TABLES FROM stats; +---- ...

  2. PHP 不让标准浏览器(firfox,chrome等)走浏览器的缓存页面

    或在HTML页面里加: <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache,no-store, must-reva ...

  3. [HTML]增加input标签的multiple属性上传的文件数

    .发现问题 <input type="file" name="myfile[]" multiple="multiple"/> 最 ...

  4. ZOJ 1610 Count the Colors(区间染色)

    题目大意:多组数据,每组给一个n(1=<n<=8000),下面有n行,每行有l,r,color(1=<color<=8000),表示将l~r颜色变为color,最后求各种颜色( ...

  5. 包含Winsock2.h出错问题

    工程中添加 Winsock2.h 报错 1>c:\program files (x86)\windows kits\8.1\include\shared\ws2def.h(100): warni ...

  6. Lab 4 in Tornado

    反正也没给CSS,自己改了下样式…… 效果: 题目给的验证信用卡号码规则不太全,万事达的卡第二位必须是1~5,另外其实visa号码也有13位的……要兼容这个的话只要把正则改成'^4([0-9]{12, ...

  7. 解决 .net HttpClient 调用时出现的 "A task was cancelled" 错误

    近日在系统中集成ElasticClient客户端,自动创建索引.删除索引,发现通过 ElasticClient 的 LowerLevelClient 无法正确返回结果,但是索引已成功创建或删除. 并会 ...

  8. pyqt5改变窗体颜色

    from PyQt5.QtWidgets import QApplication,QWidget from PyQt5.QtGui import QColor import sys from t im ...

  9. Recursion in Java

    Recursion in Java 递归无出口 public class RecursionExample1 { public static void p() { System.out.println ...

  10. learn_requests

    # -*- coding: utf-8 -*- import requests URL_IP = 'http://localhost:8000/ip' URL_GET = 'http://localh ...