简体   繁体   English

Python 相当于 R 的 dput() 函数

[英]Python's equivalent for R's dput() function

python 中有没有类似于 R 中的dput() 函数的函数

用于pandas.DataFrameprint(df.to_dict()) ,如图这里

There are several options for serializing Python objects to files:有几个选项可以将 Python 对象序列化为文件:

  • json.dump() stores the data in JSON format. json.dump()以 JSON 格式存储数据。 It is very read- and editable, but can only store lists, dicts, strings, numbers, booleans, so no compound objects.它具有很强的可读性和可编辑性,但只能存储列表、字典、字符串、数字、布尔值,因此不能存储复合对象。 You need to import json before to make the json module available.您需要先import json才能使json模块可用。
  • pickle.dump() can store most objects. pickle.dump()可以存储大多数对象。

Less common:不常见:

  • The shelve module stores multiple Python objects in a DBM database, mostly acting like a persistent dict . shelve模块在 DBM 数据库中存储多个 Python 对象,主要充当持久dict
  • marshal.dump() : Not sure when you'd ever need that. marshal.dump() :不确定何时需要它。

This answer focuses on json.dump() and json.dumps() and how to use them with numpy arrays.这个答案侧重于json.dump()json.dumps()以及如何将它们与 numpy 数组一起使用。 If you try, Python will hit you with an error saying that ndarrays are not JSON serializable:如果你尝试,Python 会告诉你一个错误,说 ndarrays 不是 JSON 可序列化的:

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()似乎与 R 的dput()最接近,因为它允许您直接从控制台复制粘贴结果:

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()dput()但它仍然非常有用。 json.dump() will encode your object to a json file. json.dump()将您的对象编码为 json 文件。

# 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:然后你可以再次将它转换回一个 numpy 数组:

c = np.array(c)

More information更多信息

on avoiding the 'not serializable' error see:关于避免“不可序列化”错误,请参阅:

How no one has mentioned repr() yet is a mystery to me.怎么没人提到repr()对我来说是个谜。 repr() does almost exactly what R's dput() does. repr()几乎与 R 的dput()所做的完全一样。 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)'

IMO, json.dumps() (注意s )甚至更好,因为它返回一个字符串,而不是json.dump()要求您写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM