pprint

pprint(): how to use double quotes to display strings?

帅比萌擦擦* 提交于 2019-11-29 09:59:21
If I print a dictionary using pprint , it always wraps strings around single quotes ( ' ): >>> from pprint import pprint >>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3}) {'AAA': 1, 'BBB': 2, 'CCC': 3} Is there any way to tell pprint to use double quotes ( " ) instead? I would like to have the following behaviour: >>> from pprint import pprint >>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3}) {"AAA": 1, "BBB": 2, "CCC": 3} It looks like you are trying to produce JSON; if so, use the json module : >>> import json >>> print json.dumps({'AAA': 1, 'BBB': 2, 'CCC': 3}) {"AAA": 1, "BBB": 2, "CCC": 3} The pprint()

Pretty print JSON python

十年热恋 提交于 2019-11-28 19:41:41
if anybody with some knowledge about pretty printing JSON could help me with this I would be extremely grateful! I'm looking to convert a complex python string into JSON format, using the function below to move the JSON string to a file: with open('data.txt', 'wt') as out: pprint(string, stream=out) The problem is that I'm getting syntax errors for the square brackets I believe, as this is a new topic for me and I can't figure out how to get around this. The JSON format I require is like this: { cols:[{id: 'Time', "label":"Time","type": "datetime"}, {id: 'Time', "label":"Latency","type":

pprint dictionary on multiple lines

£可爱£侵袭症+ 提交于 2019-11-28 06:41:16
I'm trying to get a pretty print of a dictionary, but I'm having no luck: >>> import pprint >>> a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}} >>> pprint.pprint(a) {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}} I wanted the output to be on multiple lines, something like this: {'first': 123, 'second': 456, 'third': {1: 1, 2: 2} } Can pprint do this? If not, then which module does it? I'm using Python 2.7.3 . Use width=1 or width=-1 : In [33]: pprint.pprint(a, width=1) {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}} You could convert the dict to json through json.dumps(d,

Any way to properly pretty-print ordered dictionaries?

北慕城南 提交于 2019-11-28 03:44:26
I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window. It has worked fine until they added the new ordered dictionary type in Python 2.7 (another cool feature I really like). If I try to pretty-print an ordered dictionary, it doesn't show nicely. Instead of having each key-value pair on its own line, the whole thing shows up on one long line, which wraps many times and is hard to read. Does anyone here have a way to make it print nicely, like the old unordered dictionaries? I

pretty-printing a record using a custom method in Clojure

帅比萌擦擦* 提交于 2019-11-28 03:29:32
问题 In Clojure 1.5.0, how can I provide a custom pretty-printer for my own record type, defined with defrecord. (defrecord MyRecord [a b]) (defmethod print-method MyRecord [x ^java.io.Writer writer] (print-method (:a x) writer)) (defmethod print-dup MyRecord [x ^java.io.Writer writer] (print-dup (:a x) writer)) (println (MyRecord. 'a 'b)) ;; a -- OK (clojure.pprint/pprint (MyRecord. 'a 'b)) ;; {:a a, :b b} -- not OK, I want a I would like clojure.pprint/pprint to also use my cutsom printer (which

Parsing json and searching through it

匆匆过客 提交于 2019-11-27 19:28:37
I have this code import json from pprint import pprint json_data=open('bookmarks.json') jdata = json.load(json_data) pprint (jdata) json_data.close() How can I search through it for u'uri': u'http: ? As json.loads simply returns a dict, you can use the operators that apply to dicts: >>> jdata = json.load('{"uri": "http:", "foo", "bar"}') >>> 'uri' in jdata # Check if 'uri' is in jdata's keys True >>> jdata['uri'] # Will return the value belonging to the key 'uri' u'http:' Edit: to give an idea regarding how to loop through the data, consider the following example: >>> import json >>> jdata =

Pretty print JSON python

梦想与她 提交于 2019-11-27 12:27:57
问题 if anybody with some knowledge about pretty printing JSON could help me with this I would be extremely grateful! I'm looking to convert a complex python string into JSON format, using the function below to move the JSON string to a file: with open('data.txt', 'wt') as out: pprint(string, stream=out) The problem is that I'm getting syntax errors for the square brackets I believe, as this is a new topic for me and I can't figure out how to get around this. The JSON format I require is like this

Any way to properly pretty-print ordered dictionaries?

早过忘川 提交于 2019-11-27 00:05:00
问题 I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window. It has worked fine until they added the new ordered dictionary type in Python 2.7 (another cool feature I really like). If I try to pretty-print an ordered dictionary, it doesn't show nicely. Instead of having each key-value pair on its own line, the whole thing shows up on one long line, which wraps many times and is