python 字符串处理
程序员文章站
2022-05-10 23:06:19
...
python 把字符串 转换成 字典
a={"cardtype":"A711","dt":"1447223787","token":"6C7C75327CC6FB4C77051E2BBD85CFAF","appid":"13a876d53ee4da1a","tid":"17bf1867aa5d4d8e8c0f15a197cb9db5","imsi":"460011082618869"}
type(a) --> str
1.
b= eval(a) ; type(b) --> dict
2.
import json
c = josn.loads(a) ;type(c) --> dict
-----------------------------------------------------------------------------------------
把字典类型转换成 json 形式输出
import json
v1 = json.load(a)
v2 = json.dumps(v1,sort_keys=True,indent=4)
print v2
---输出如下:
{
"appid": "13a876d53ee4da1a",
"cardtype": "A711",
"dt": "1447223787",
"imsi": "460011082618869",
"tid": "17bf1867aa5d4d8e8c0f15a197cb9db5",
"token": "6C7C75327CC6FB4C77051E2BBD85CFAF"
}
a={"cardtype":"A711","dt":"1447223787","token":"6C7C75327CC6FB4C77051E2BBD85CFAF","appid":"13a876d53ee4da1a","tid":"17bf1867aa5d4d8e8c0f15a197cb9db5","imsi":"460011082618869"}
type(a) --> str
1.
b= eval(a) ; type(b) --> dict
2.
import json
c = josn.loads(a) ;type(c) --> dict
-----------------------------------------------------------------------------------------
把字典类型转换成 json 形式输出
import json
v1 = json.load(a)
v2 = json.dumps(v1,sort_keys=True,indent=4)
print v2
---输出如下:
{
"appid": "13a876d53ee4da1a",
"cardtype": "A711",
"dt": "1447223787",
"imsi": "460011082618869",
"tid": "17bf1867aa5d4d8e8c0f15a197cb9db5",
"token": "6C7C75327CC6FB4C77051E2BBD85CFAF"
}
上一篇: Oracle 索引简单总结
下一篇: 实现简易html视频播放器的方法