1

我想从嵌套在列表中的字典中漂亮地打印出每个键值对。所以这就是我正在使用的:

[{"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":"" 
     }
]
4

2 回答 2

3

想到了一种可能性。您是否向 提供 JSON 字符串pprint?如果是这样,您应该首先对其进行解码:

pprint(json.loads(data))
于 2011-09-27T22:52:00.210 回答
1

它嗯...适合我。(我剪切并粘贴了您的示例!)您使用的是什么 Python 和操作系统?你确定你没有不小心在某处添加了一些额外的引号吗?

您要打印到终端吗?

于 2011-09-27T22:48:16.410 回答