ensnare ensnare - 9 days ago 9
PHP Question

What is the equivalent of php's print_r() in python?

Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.

Answer

The python print statement does a good job of formatting multidimesion arrays without requiring the print_r available in php.

As the definition for print states that each object is converted to a string, and as simple arrays print a '[' followed by a comma separated list of object values followed by a ']', this will work for any depth and shape of arrays.

For example

>>> x = [[1,2,3],[4,5,6]]
>>> print x
[[1, 2, 3], [4, 5, 6]]

If you need more advanced formatting than this, AJs answer suggesting pprint is probably the way to go.

Comments