Is there any function in python similar to dput() function in R?
python中的任何函数是否与R中的dput()函数类似?
3 个解决方案
#1
6
There are several options for serializing Python objects to files:
将Python对象序列化为文件有几种选择:
-
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. - json.dump()以JSON格式存储数据。它非常易读和可编辑,但只能存储列表,字符串,字符串,数字,布尔值,因此不存在复合对象。您需要先导入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 persistentdict
. - shelve模块在DBM数据库中存储多个Python对象,其中大部分都像持久性字典一样。
-
marshal.dump()
: Not sure when you'd ever need that. - marshal.dump():不确定你什么时候需要它。
#2
0
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.
IMO,json.dumps()(注意s)甚至更好,因为它返回一个字符串,而不是json.dump(),它需要你写一个文件。
#3
0
for a pandas.DataFrame
, print(df.to_dict())
, as shown here
对于pandas.DataFrame,print(df.to_dict()),如下所示
#1
6
There are several options for serializing Python objects to files:
将Python对象序列化为文件有几种选择:
-
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. - json.dump()以JSON格式存储数据。它非常易读和可编辑,但只能存储列表,字符串,字符串,数字,布尔值,因此不存在复合对象。您需要先导入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 persistentdict
. - shelve模块在DBM数据库中存储多个Python对象,其中大部分都像持久性字典一样。
-
marshal.dump()
: Not sure when you'd ever need that. - marshal.dump():不确定你什么时候需要它。
#2
0
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.
IMO,json.dumps()(注意s)甚至更好,因为它返回一个字符串,而不是json.dump(),它需要你写一个文件。
#3
0
for a pandas.DataFrame
, print(df.to_dict())
, as shown here
对于pandas.DataFrame,print(df.to_dict()),如下所示