How do I use Python's pprint module to pretty print out dictionary key value pairs nested with in a list?
I would like to pretty print out each key value pair from dictionaries which are nested in a list. So here is what I am working with:
[{"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":""}]
When I do
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)
the result I get is the same as the original list but in a string
'[{"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":""}]'
How do I make it pretty print the data to look something like this?
[
{
"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":"2开发者_开发问答011/09/26 11:05:48 +0000",
"title":"Polimex - Plastic\xc2\xa0accessories",
"comments":[],"desc":""
}
]
One possibility comes to mind. Are you supplying a JSON string to pprint
? If so, you should first decode it:
pprint(json.loads(data))
It um... works right for me. (I cut and pasted your example!) What Python and OS are you using? And are you sure you didn't accidentally add some additional quotes somewhere?
Are you printing to the terminal?
精彩评论