Is there any function in python similar to dput() function in R?
5 Answers
for a pandas.DataFrame
, print(df.to_dict())
, as shown here.
And back again with df = pandas.DataFrame.from_dict(data_as_dict)

- 10,037
- 9
- 76
- 111
-
2
-
@PatrickT, do you know how to do that with other objects such as `dictionaries`? – Álvaro A. Gutiérrez-Vargas Feb 09 '23 at 17:28
-
If I understand your question, `print(d)` will do that. You can also output the keys and the values separately with `d.keys()` and `d.values()`. Maybe your question is more involved? Look at this perhaps: https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries – PatrickT Feb 09 '23 at 17:48
There are several options for serializing Python objects to files:
json.dump()
stores the data in JSON format. It is very read- and editable, but can only store lists, dicts, strings, numbers, booleans, so no compound objects. You need toimport json
before to make thejson
module available.pickle.dump()
can store most objects.
Less common:
- The
shelve
module stores multiple Python objects in a DBM database, mostly acting like a persistentdict
. marshal.dump()
: Not sure when you'd ever need that.

- 6,989
- 4
- 40
- 60
-
1As this is a beginner's question would you please clarify if it requires to ``import json`` or something similar. Also I tried it on a ``pandas.DataFrame`` and got ``dump() missing 1 required positional argument: 'fp'`` ... – PatrickT Apr 20 '18 at 05:10
-
1Could you illustrate with an example, @ChristianAichinger? I agree with what @PatrickT said since I am getting the same error `dump() missing 1 required positional argument: 'fp'` – Álvaro A. Gutiérrez-Vargas Feb 09 '23 at 17:31
How no one has mentioned repr()
yet is a mystery to me. repr()
does almost exactly what R's dput()
does. Here's a few examples:
>>> a = np.arange(10)
>>> repr(a)
'array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
>>> d = dict(x=1, y=2)
>>> repr(d)
"{'x': 1, 'y': 2}"
>>> b = range(10)
>>> repr(b)
'range(0, 10)'

- 792
- 5
- 16
-
1
-
3It is still inferior to `dput` because it does not keep the data type of the columns :/ – user3507584 Aug 30 '21 at 15:07
This answer focuses on json.dump()
and json.dumps()
and how to use them with numpy arrays. If you try, Python will hit you with an error saying that ndarrays are not JSON serializable:
import numpy as np
import json
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
json.dumps(a)
TypeError: Object of type 'ndarray' is not JSON serializable
You can avoid this by translating it to a list first. See below for two working examples:
json.dumps()
json.dumps()
seems to be the closest to R's dput()
since it allows you to copy-paste the result straight from the console:
json.dumps(a.tolist()) # '[[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
json.dump()
json.dump()
is not the same as dput()
but it's still very useful. json.dump()
will encode your object to a json file.
# Encode:
savehere = open('file_location.json', 'w')
json.dump(a.tolist(), savehere)
which you can then decode elsewhere:
# Decode:
b = open('file_location.json', 'r').read() # b is '[[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
c = json.loads(b)
Then you can transform it back a numpy array again:
c = np.array(c)
More information
on avoiding the 'not serializable' error see:
how to make classes json serializable (kind of unrelated, but very interesting)

- 6,756
- 6
- 37
- 52
-
Thanks, what would be the correct parameter to obtain matrices formatted with one line per row in the matrix, as the standard `numpy.array` output? I tried to pass the `indent` and `separators` parameters to `json.dumps` without success. – Paul Rougieux Mar 15 '20 at 15:27
IMO, json.dumps()
(note the s) is even better since it returns a string, as opposed to json.dump()
which requires you to write to a file.