Dan's solution is just wrong, and Ismail's in incomplete.
__str__()is not called,__repr__()is called.__repr__()should return a string, as pformat does.- print normally indents only 1 character and tries to save lines. If you are trying to figure out structure, set the width low and indent high.
Here is an example
class S:
def __repr__(self):
from pprint import pformat
return pformat(vars(self), indent=4, width=1)
a = S()
a.b = 'bee'
a.c = {'cats': ['blacky', 'tiger'], 'dogs': ['rex', 'king'] }
a.d = S()
a.d.more_c = a.c
print(a)
This prints
{ 'b': 'bee',
'c': { 'cats': [ 'blacky',
'tiger'],
'dogs': [ 'rex',
'king']},
'd': { 'more_c': { 'cats': [ 'blacky',
'tiger'],
'dogs': [ 'rex',
'king']}}}
Which is not perfect, but passable.
Answer from Charles Merriam on Stack OverflowDan's solution is just wrong, and Ismail's in incomplete.
__str__()is not called,__repr__()is called.__repr__()should return a string, as pformat does.- print normally indents only 1 character and tries to save lines. If you are trying to figure out structure, set the width low and indent high.
Here is an example
class S:
def __repr__(self):
from pprint import pformat
return pformat(vars(self), indent=4, width=1)
a = S()
a.b = 'bee'
a.c = {'cats': ['blacky', 'tiger'], 'dogs': ['rex', 'king'] }
a.d = S()
a.d.more_c = a.c
print(a)
This prints
{ 'b': 'bee',
'c': { 'cats': [ 'blacky',
'tiger'],
'dogs': [ 'rex',
'king']},
'd': { 'more_c': { 'cats': [ 'blacky',
'tiger'],
'dogs': [ 'rex',
'king']}}}
Which is not perfect, but passable.
pprint.pprint doesn't return a string; it actually does the printing (by default to stdout, but you can specify an output stream). So when you write print record, record.__str__() gets called, which calls pprint, which returns None. str(None) is 'None', and that gets printed, which is why you see None.
You should use pprint.pformat instead. (Alternatively, you can pass a StringIO instance to pprint.)
Pretty print JSON from URL
New Protocol for Pretty Printing
Format specifier for pretty printing - Ideas - Discussions on Python.org
Printing a class object?
Videos
Hello Everyone,
I've hit a frustrating wall and after much googling I took a deep breath and felt the best approach was to ask you all for advice.
I'm making a very simple script and need assistance with what tool I should use to move forward.
Step 1: Fetch the URL Step 2: Make the JSON pretty. Such as a top-down listing of Title and URL only.
import requests
#make the URL call
page = requests.get('http://gleamlist.com:5000/api')
#verify the page has content - just for testing.
print(page.content)I've tried using the json module but confuse myself and I've thought maybe BS4 would help but also confuse myself. I believe JSON is basically a dictionary and I want to parse out the info. Could anyone assist me with some tips or a specific topic to research and implement please?
I've recently documented a protocol in Rich to add pretty printing to arbitrary objects.
The problem was that containers like dicts, lists, sets etc could be formatted over multiple lines, but custom classes are limited to a string that can be generated from __repr__. The new protocol in Rich adds a __rich_repr__ method which allows an object to declare how it should be pretty printed.
Looking for feedback. Here are the docs.
Here's an example of a __rich_repr__ method.
And here's what it looks like when you pretty print a Bird instance:
I'm trying to use the pokebase wrapper just to explore and I'm really struggling with it.
I just want to print all the moves a pokemon has but they show up as locations in memory instead of the actual values. I'm not sure how to get the data out of this.
import pokebase as pb pokemon = pb.pokemon(1) print(pokemon.moves)
Example of results:
[<pokebase.interface.APIMetadata object at 0x000001E774593C70>, <pokebase.interface.APIMetadata object at 0x000001E774543940>, <pokebase.interface.APIMetadata object at 0x000001E774593B20>]
The pprint module has a function named pformat, for just that purpose.
From the documentation:
Return the formatted representation of object as a string. indent, width and depth will be passed to the PrettyPrinter constructor as formatting parameters.
Example:
>>> import pprint
>>> people = [
... {"first": "Brian", "last": "Kernighan"},
... {"first": "Dennis", "last": "Richie"},
... ]
>>> pprint.pformat(people, indent=4)
"[ { 'first': 'Brian', 'last': 'Kernighan'},\n { 'first': 'Dennis', 'last': 'Richie'}]"
Assuming you really do mean pprint from the pretty-print library, then you want
the pprint.pformat function.
If you just mean print, then you want str()