pprint

How to format dictionary with pprint library?

How to format dictionary? Question: I have this function: def total_fruit_per_sort(): number_found = re.findall(total_amount_fruit_regex(), verdi50) fruit_dict = {} for n, f in number_found: fruit_dict[f] = fruit_dict.get(f, 0) + int(n) return pprint.pprint(str( {value: key for value, key in fruit_dict.items() }).replace("{", "").replace("}", "").replace("’", "")) print(total_fruit_per_sort()) But it prints the values like: ‘Watermeloenen: 466, Appels: 688, Sinaasappels: 803’ …

Total answers: 2

Why did I get ModuleNotFoundError?

Why did I get ModuleNotFoundError? Question: I tried the following code to import pprint from pprint module like import matplotlib.pyplot as plt. But I got an error instead. Why did I get this error? import pprint.pprint as p ————————————————————————— ModuleNotFoundError Traceback (most recent call last) <ipython-input-10-029262df2bb9> in <module> —-> 1 import pprint.pprint as p ModuleNotFoundError: …

Total answers: 1

Can I avoid a sorted dictionary output after I've used pprint.pprint, in Python?

Can I avoid a sorted dictionary output after I've used pprint.pprint, in Python? Question: The code is: from pprint import pprint d = {“b” : “Maria”, “c” : “Helen”, “a” : “George”} pprint(d, width = 1) The output is: {‘a’: ‘George’, ‘b’: ‘Maria’, ‘c’: ‘Helen’} But, the desired output is: {‘b’: ‘Maria’, ‘c’: ‘Helen’, ‘a’: …

Total answers: 5

pprint sorting dicts but not sets?

pprint sorting dicts but not sets? Question: I know that dicts and sets aren’t ordered, so equal sets or dicts may print differently (all tests with Python 3.6.1): >>> for obj in {0, 8}, {8, 0}, {0:0, 8:8}, {8:8, 0:0}: print(obj) {0, 8} {8, 0} {0: 0, 8: 8} {8: 8, 0: 0} And I …

Total answers: 1

Disabling sorting mechanism in pprint output

Disabling sorting mechanism in pprint output Question: I have big dictionary which I`m printing for viewing with prettyprint, but how I can keep formatting but kill sorting mechanism in pprint? Asked By: Edd || Source Answers: Python 3.8 or newer: Use sort_dicts=False: pprint.pprint(data, sort_dicts=False) Python 3.7 or older: You can monkey patch the pprint module. …

Total answers: 5

pprint dictionary on multiple lines

pprint dictionary on multiple lines Question: 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 …

Total answers: 5

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

pprint(): how to use double quotes to display strings? Question: 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 (“) …

Total answers: 3

Use logging print the output of pprint

Use logging print the output of pprint Question: I want to use pprint’s output to show a complex data structure, but I would like to output it using the logging module rather than stdout. ds = [{‘hello’: ‘there’}] logging.debug( pprint.pprint(ds) ) # outputs as STDOUT Asked By: yee379 || Source Answers: Use pprint.pformat to get …

Total answers: 3

How to use pprint to print an object using the built-in __str__(self) method?

How to use pprint to print an object using the built-in __str__(self) method? Question: I have a Python script which processes a .txt file which contains report usage information. I’d like to find a way to cleanly print the attributes of an object using pprint’s pprint(vars(object)) function. The script reads the file and creates instances …

Total answers: 6

How do I get python's pprint to return a string instead of printing?

How do I get python's pprint to return a string instead of printing? Question: In other words, what’s the sprintf equivalent to pprint? Asked By: drue || Source Answers: 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 …

Total answers: 5