pretty-print

Printing boolean numpy array without separators

Printing boolean numpy array without separators Question: I would like to print this array: a = np.array([[0, 1, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=bool) as .8.. 8888 …. …. without iterating over each element in a double loop. A terse function like this one: def showGrid(g): …

Total answers: 1

Is there a way to pretty print / prettify a JSON response in FastAPI?

Is there a way to pretty print / prettify a JSON response in FastAPI? Question: I’m looking for something that is similar to Flask’s app.config[‘JSONIFY_PRETTYPRINT_REGULAR’] = True. Asked By: New Coder || Source Answers: I’m not sure what exactly your problem is, can you tell what the background of your need? however, because FASTAPI is …

Total answers: 3

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

How do I get Python's ElementTree to pretty print to an XML file?

How do I get Python's ElementTree to pretty print to an XML file? Question: Background I am using SQLite to access a database and retrieve the desired information. I’m using ElementTree in Python version 2.6 to create an XML file with that information. Code import sqlite3 import xml.etree.ElementTree as ET # NOTE: Omitted code where …

Total answers: 8

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

Print a list of space-separated elements

Print a list of space-separated elements Question: I have a list L of elements, say natural numbers. I want to print them in one line with a single space as a separator. But I don’t want a space after the last element of the list (or before the first). In Python 2, this can easily …

Total answers: 4

How to pretty print in ipython notebook via sympy?

How to pretty print in ipython notebook via sympy? Question: I tried pprint, print, the former only prints Unicode version, and the latter doesn’t do pretty prints. from sympy import symbols, Function import sympy.functions as sym from sympy import init_printing init_printing(use_latex=True) from sympy import pprint from sympy import Symbol x = Symbol(‘x’) # If a …

Total answers: 4

Prettyprint to a file?

Prettyprint to a file? Question: I’m using this gist’s tree, and now I’m trying to figure out how to prettyprint to a file. Any tips? Asked By: James.Wyst || Source Answers: What you need is Pretty Print pprint module: from pprint import pprint # Build the tree somehow with open(‘output.txt’, ‘wt’) as out: pprint(myTree, stream=out) …

Total answers: 4

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

Custom indent width for BeautifulSoup .prettify()

Custom indent width for BeautifulSoup .prettify() Question: Is there any way to define custom indent width for .prettify() function? From what I can get from it’s source – def prettify(self, encoding=None, formatter=”minimal”): if encoding is None: return self.decode(True, formatter=formatter) else: return self.encode(encoding, True, formatter=formatter) There is no way to specify indent width. I think it’s …

Total answers: 4