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とOSを使用していますか?そして、誤ってどこかに引用符を追加しなかったと思いますか?

ターミナルに印刷していますか?

于 2011-09-27T22:48:16.410 に答える