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

PhpPython

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

You were looking for the repr bult-in function.
http://docs.python.org/2/library/functions.html#func-repr

print repr(variable)

In Python 3, print is no longer a statement, so that would be:

print( repr(variable) )

Solution 3 - Php

from pprint import pprint

student = {'Student1': { 'Age':10, 'Roll':1 }, 
		   'Student2': { 'Age':12, 'Roll':2 }, 
		   'Student3': { 'Age':11, 'Roll':3 }, 
		   'Student4': { 'Age':13, 'Roll':4 }, 
		   'Student5': { 'Age':10, 'Roll':5 }
		   }
		   
pprint(student)

Solution 4 - Php

here's one you can try:

https://github.com/sha256/python-var-dump

you can install it simply using pip

pip install var_dump

disclaimer: I wrote it :)

Solution 5 - Php

print and pprint are great for built-in data types or classes which define a sane object representation. If you want a full dump of arbitrary objects, you'll have to roll your own. That is not that hard: simply create a recursive function with the base case being any non-container built-in data type, and the recursive case applying the function to each item of a container or each attribute of the object, which can be gotten using dir() or the inspect module.

Solution 6 - Php

there is print_r for python https://github.com/marcbelmont/python-print_r/wiki but it is better to use standard modules

Solution 7 - Php

my_list = list(enumerate([1,2,3,4,5,6,7,8,9],0))

print(my_list)

Will print [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]

Solution 8 - Php

if you want to format variable as a string you can do:

s = repr(variable)

it works regardless the type, and doesn't requiere any imports.

If you want to include the contents of an object in a single string together with its type:

if hasattr(variable, "__dict__"):
    s = "{}: {}".format(variable, vars(variable))
else:
    s = repr(variable)

Solution 9 - Php

For simple test views I use HttpResponse. Testing request from form, variable, string, id, not Object

from django.http.response import HttpResponse
HttpResponse(variable)

def add_comment(request, id):
return HttpResponse(id)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionensnareView Question on Stackoverflow
Solution 1 - PhpRobert ChristieView Answer on Stackoverflow
Solution 2 - PhpGetFreeView Answer on Stackoverflow
Solution 3 - PhpMukesh ChapagainView Answer on Stackoverflow
Solution 4 - Phpsha256View Answer on Stackoverflow
Solution 5 - PhpMax ShawabkehView Answer on Stackoverflow
Solution 6 - PhpjellyfishView Answer on Stackoverflow
Solution 7 - PhpStephan van der HulstView Answer on Stackoverflow
Solution 8 - PhpAlejandroVDView Answer on Stackoverflow
Solution 9 - PhpIvan PirusView Answer on Stackoverflow