如何使用Python的pprint模块漂亮地打印嵌套在列表中的字典键值对?

1 投票
2 回答
10686 浏览
提问于 2025-04-17 03:14

我想把一个列表里面的字典中的每一个键值对都漂亮地打印出来。这里是我正在处理的内容:

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

2 个回答

1

嗯…对我来说,这个是有效的。(我复制并粘贴了你的例子!)你用的是什么Python和操作系统?你确定没有不小心在某个地方多加了引号吗?

你是在终端上打印输出吗?

3

有一个可能性可以考虑。你是不是把一个JSON字符串传给了pprint?如果是这样的话,你应该先把它解码:

pprint(json.loads(data))

撰写回答