Comment puis-je utiliser Python pprint module pour la jolie imprimer dictionnaire de paires clé-valeur imbriquées dans une liste?

J'aimerais assez d'imprimer chaque valeur de la clé de la paire de dictionnaires qui sont imbriquées dans une liste. Voici donc ce que je suis en train de travailler avec:

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

Quand je fais

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)

le résultat que j'obtiens est le même que l'original de la liste, mais dans une chaîne

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

Comment puis-je faire joli imprimer les données à ressembler à quelque chose comme ça?

[
    {
     "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":"" 
     }
]
  • J'ai juste essayé ce à partir du shell interactif (littéralement collé votre code, ci-dessus) et a obtenu le résultat attendu. Je ne peux pas imaginer comment vous obtenez une chaîne de pprint, puisqu'elle retourne Aucun.
  • J'ai foiré. J'ai oublié que le data liste a été enveloppé dans des guillemets. Je suis désolé pour cette erreur. Quand j'enlève les guillemets il pprints comme prévu.
InformationsquelleAutor | 2011-09-27