我想从嵌套在列表中的字典中漂亮地打印出每个键值对。所以这就是我正在使用的:
[{"updated_at":"2011/09/26 22:39:18 +0000","url":"http://diveintopython.net/http_web_services/redirects.html","annotations":[],"user":"name","shared":"yes","tags":"python,handler,opener,urllib2","readlater":"no","created_at":"2011/09/26 22:39:18 +0000","title":"11.7.\xc2\xa0Handling redirects","comments":[],"desc":""},{"updated_at":"2011/09/26 11:09:07 +0000","url":"http://www.polimex.net/sklep/index.php?page=Category&catid=7","annotations":[],"user":"name","shared":"yes","tags":"plastic,snap,buttons,clothing","readlater":"no","created_at":"2011/09/26 11:05:48 +0000","title":"Polimex - Plastic\xc2\xa0accessories","comments":[],"desc":""}]
当我做
from pprint import pprint
data = [{"updated_at":"2011/09/26 22:39:18 +0000","url":"http://diveintopython.net/http_web_services/redirects.html","annotations":[],"user":"name","shared":"yes","tags":"python,handler,opener,urllib2","readlater":"no","created_at":"2011/09/26 22:39:18 +0000","title":"11.7.\xc2\xa0Handling redirects","comments":[],"desc":""},{"updated_at":"2011/09/26 11:09:07 +0000","url":"http://www.polimex.net/sklep/index.php?page=Category&catid=7","annotations":[],"user":"name","shared":"yes","tags":"plastic,snap,buttons,clothing","readlater":"no","created_at":"2011/09/26 11:05:48 +0000","title":"Polimex - Plastic\xc2\xa0accessories","comments":[],"desc":""}]
pprint(data)
我得到的结果与原始列表相同,但在一个字符串中
'[{"updated_at":"2011/09/26 22:39:18 +0000","url":"http://diveintopython.net/http_web_services/redirects.html","annotations":[],"user":"name","shared":"yes","tags":"python,handler,opener,urllib2","readlater":"no","created_at":"2011/09/26 22:39:18 +0000","title":"11.7.\xc2\xa0Handling redirects","comments":[],"desc":""},{"updated_at":"2011/09/26 11:09:07 +0000","url":"http://www.polimex.net/sklep/index.php?page=Category&catid=7","annotations":[],"user":"name","shared":"yes","tags":"plastic,snap,buttons,clothing","readlater":"no","created_at":"2011/09/26 11:05:48 +0000","title":"Polimex - Plastic\xc2\xa0accessories","comments":[],"desc":""}]'
如何让它漂亮地打印数据看起来像这样?
[
{
"updated_at":"2011/09/26 22:39:18 +0000",
"url":"http://diveintopython.net/http_web_services/redirects.html",
"annotations":[],
"user":"name",
"shared":"yes",
"tags":"python,handler,opener,urllib2",
"readlater":"no",
"created_at":"2011/09/26 22:39:18 +0000",
"title":"11.7.\xc2\xa0Handling redirects",
"comments":[],
"desc":""
},
{
"updated_at":"2011/09/26 11:09:07 +0000",
"url":"http://www.polimex.net/sklep/index.php?page=Category&catid=7",
"annotations":[],
"user":"name",
"shared":"yes",
"tags":"plastic,snap,buttons,clothing",
"readlater":"no",
"created_at":"2011/09/26 11:05:48 +0000",
"title":"Polimex - Plastic\xc2\xa0accessories",
"comments":[],"desc":""
}
]