@nalan90
2017-07-31T11:57:26.000000Z
字数 3185
阅读 576
Python高效编程技巧实战
常用函数
代码示例
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
json_data = {
"_id": "597afc86037ae2cf099b7675",
"index": 1,
"guid": "cf490eac-aa4b-457f-9885-15cc561a6398",
"isActive": False,
"balance": "$1,549.54",
"picture": "http://placehold.it/32x32",
"age": 33,
"latitude": -35.199476,
"longitude": 42.811808,
"tags": [
"fugiat",
"ex",
"incididunt",
"voluptate",
"veniam",
"magna",
"ea"
],
"friends": [
{
"id": 0,
"name": "Snider Cummings"
},
{
"id": 2,
"name": "Vance Wilder"
}
],
"favoriteFruit": "strawberry"
}
print type(json_data)
print 'convert python object to json string'
## 将python object转化成json string
json_str = json.dumps(json_data)
print json_str
## 将python object json化处理后写入文件
print 'convert python object to json strings then write them to file'
with open('json.demo','w+') as f:
json.dump(json_data,f)
print '-' * 100
## 将json string 转化成python object
print json.loads(json_str)
## 从文件中读取json string,并转化成python object
print 'read json strings from file then convert them to python object'
with open('json.demo','r+') as f:
print json.load(f)
---------------------------------------------------------------------------------------
## 运行结果如下:
(/anaconda) work:python ys$ python json_demo.py
<type 'dict'>
## convert python object to json string
{"picture": "http://placehold.it/32x32", "tags": ["fugiat", "ex", "incididunt", "voluptate", "veniam", "magna", "ea"], "guid": "cf490eac-aa4b-457f-9885-15cc561a6398", "friends": [{"id": 0, "name": "Snider Cummings"}, {"id": 2, "name": "Vance Wilder"}], "isActive": false, "index": 1, "_id": "597afc86037ae2cf099b7675", "age": 33, "longitude": 42.811808, "favoriteFruit": "strawberry", "latitude": -35.199476, "balance": "$1,549.54"}
--------------------------------------------------------------------------------------
## convert json string to python object
{u'picture': u'http://placehold.it/32x32', u'_id': u'597afc86037ae2cf099b7675', u'index': 1, u'age': 33, u'tags': [u'fugiat', u'ex', u'incididunt', u'voluptate', u'veniam', u'magna', u'ea'], u'longitude': 42.811808, u'favoriteFruit': u'strawberry', u'latitude': -35.199476, u'balance': u'$1,549.54', u'guid': u'cf490eac-aa4b-457f-9885-15cc561a6398', u'friends': [{u'id': 0, u'name': u'Snider Cummings'}, {u'id': 2, u'name': u'Vance Wilder'}], u'isActive': False}
## read data from file and convert to python object
{u'picture': u'http://placehold.it/32x32', u'_id': u'597afc86037ae2cf099b7675', u'index': 1, u'age': 33, u'tags': [u'fugiat', u'ex', u'incididunt', u'voluptate', u'veniam', u'magna', u'ea'], u'longitude': 42.811808, u'favoriteFruit': u'strawberry', u'latitude': -35.199476, u'balance': u'$1,549.54', u'guid': u'cf490eac-aa4b-457f-9885-15cc561a6398', u'friends': [{u'id': 0, u'name': u'Snider Cummings'}, {u'id': 2, u'name': u'Vance Wilder'}], u'isActive': False}
## 文件中json string
(/anaconda) work:python ys$ cat json.demo
{"picture": "http://placehold.it/32x32", "tags": ["fugiat", "ex", "incididunt", "voluptate", "veniam", "magna", "ea"], "guid": "cf490eac-aa4b-457f-9885-15cc561a6398", "friends": [{"id": 0, "name": "Snider Cummings"}, {"id": 2, "name": "Vance Wilder"}], "isActive": false, "index": 1, "_id": "597afc86037ae2cf099b7675", "age": 33, "longitude": 42.811808, "favoriteFruit": "strawberry", "latitude": -35.199476, "balance": "$1,549.54"}