Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrettyPrint python into a string, and not stdout

Tags:

python

I'd like to use prettyprint to print out a dictionary, but into a string and not to console. This string is to be passed on to other functions.

I know I can use the "stream" parameter to specify a file instead of sys.out but I want a string.

How do I do that?

like image 264
eran Avatar asked Feb 26 '13 09:02

eran


1 Answers

You should simply call the pformat function from the pprint module:

import pprint s = pprint.pformat(aDict) 
like image 144
Simon Bergot Avatar answered Sep 21 '22 09:09

Simon Bergot