About 108,000 results
Open links in new tab
  1. How to print a list in Python "nicely" - Stack Overflow

    Aug 28, 2024 · For Python 3, I do the same kind of thing as shxfee's answer: def print_list(my_list): print('\n'.join(my_list)) a = ['foo', 'bar', 'baz'] print_list(a) which outputs. foo bar baz As an aside, I use a similar helper function to quickly see columns in a pandas DataFrame. def print_cols(df): print('\n'.join(df.columns))

  2. python - How to pretty print nested dictionaries? - Stack Overflow

    Feb 8, 2015 · How can I pretty print a dictionary with depth of ~4 in Python? I tried pretty printing with pprint(), but it did not work: import pprint pp = pprint.PrettyPrinter(indent=4) pp.pprint(mydict) I simply want an indentation ("\t") for each nesting, so that I get something like this: key1 value1 value2 key2 value1 value2 etc.

  3. pretty print - How do I get Python's pprint to return a string …

    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.

  4. How can I make my class pretty printable in Python?

    Nov 27, 2016 · Python has a pretty printer (pprint(...)). I would like to make my classes pretty printable. Will pretty print print my instances in a better way, if I provide a certain interface? The Python documentation in section 8.11 shows different examples, but no example how to make a user defined class pretty printable.

  5. How to print out a dictionary nicely in Python? - Stack Overflow

    May 27, 2020 · I like the pprint module (Pretty Print) included in Python. It can be used to either print the object, or format a nice string version of it. import pprint # Prints the nicely formatted dictionary pprint.pprint(dictionary) # Sets 'pretty_dict_str' to the formatted string value pretty_dict_str = pprint.pformat(dictionary)

  6. python - How to prettyprint a JSON file? - Stack Overflow

    Oct 17, 2012 · pretty-print json in python (pythonic way) 17. Pretty-printing JSON with ASCII color in python. 1. Read ...

  7. python - pretty output with pyyaml - Stack Overflow

    Oct 31, 2008 · I have a python project where I'd like to use YAML (pyYaml 3.11), particularly because it is "pretty" and easy for users to edit in a text editor if and when necessary. My problem, though, is if I bring the YAML into a python application (as I will need to) and edit the contents (as I will need to) then writing the new document is typically not ...

  8. python - Pretty-print dataclasses prettier with line breaks and ...

    Mar 26, 2021 · We can use dataclasses.fields to recurse through nested dataclasses and pretty print them: from collections.abc import Mapping, Iterable from dataclasses import is_dataclass, fields def pretty_print(obj, indent=4): """ Pretty prints a (possibly deeply-nested) dataclass. Each new block will be indented by `indent` spaces (default is 4).

  9. Python: Pretty Print in Jupyter Notebook - Stack Overflow

    Feb 27, 2017 · Any idea why this is so/ what should I change in order to see the pretty printed output? Edit: Actually this is a python 2.7 problem - it works fine in 3.x. Have anyone tried it on python 2.7 and seen it work?

  10. python - Prettyprint to a file? - Stack Overflow

    Jun 24, 2013 · Pretty print Python dictionary from command line. 9. How to print a tree in Python? 0. python: print ...

Refresh