python 中的 json.dump() 和 json.dumps() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36059194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:20:42  来源:igfitidea点击:

What is the difference between json.dump() and json.dumps() in python?

pythonjsonpython-2.7

提问by Kumaran

I searched in thisofficial document to find difference between the json.dump() and json.dumps() in python. It is clear that they are related with file write option.
But what is the detailed difference between them and in what situations one has more advantage than other?

我在这个官方文档中搜索以找到python中json.dump()和json.dumps()之间的区别。很明显,它们与文件写入选项有关。
但是它们之间的详细区别是什么?在什么情况下一个比其他更有优势?

回答by Jo?o Gon?alves

There isn't pretty much anything else to add other than what the docs say. If you want to dump the JSON into a file/socket or whatever, then you should go for dump(). If you only need it as a string (for printing, parsing or whatever) then use dumps()(dump string)

除了文档所说的内容之外,几乎没有其他要添加的内容。如果您想将 JSON 转储到文件/套接字或其他任何内容中,那么您应该选择dump(). 如果您只需要它作为字符串(用于打印、解析或其他),则使用dumps()(dump string)

As mentioned by Antii Haapala in this answer, there are some minor differences on the ensure_asciibehaviour. This is mostly due to how the underlying write()function works, being that it operates into chunks rather than the whole string. Check the answer for more details on that.

正如Antii Haapala 在这个答案中提到的,ensure_ascii行为上有一些细微的差异。这主要是由于底层write()函数的工作方式,即它操作成块而不是整个字符串。检查答案以获取更多详细信息。

json.dump()

json.dump()

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object

If ensure_ascii is False, some chunks written to fp may be unicode instances

将 obj 作为 JSON 格式的流序列化为 fp(支持 .write() 的类文件对象

如果 ensure_ascii 为 False,则写入 fp 的某些块可能是 unicode 实例

json.dumps()

json.dumps()

Serialize obj to a JSON formatted str

If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance

将 obj 序列化为 JSON 格式的 str

如果 ensure_ascii 为 False,则结果可能包含非 ASCII 字符,返回值可能是 unicode 实例

回答by Pratik Gujarathi

The functions with an stake string parameters. The others take file streams.

带有s带字符串参数的函数。其他人采用文件流。

回答by JenyaKh

In memory usage and speed.

在内存使用和速度方面。

When you call jsonstr = json.dumps(mydata)it first creates a full copy of your data in memory and only then you file.write(jsonstr)it to disk. So this is a faster method but can be a problem if you have a big piece of data to save.

当您调用jsonstr = json.dumps(mydata)它时,首先会在内存中创建数据的完整副本,然后才将file.write(jsonstr)其创建到磁盘。因此,这是一种更快的方法,但如果您要保存大量数据,则可能会出现问题。

When you call json.dump(mydata, file)-- without 's', new memory is not used, as the data is dumped by chunks. But the whole process is about 2 times slower.

当您调用json.dump(mydata, file)-- 不带 's' 时,不会使用新内存,因为数据是按块转储的。但整个过程大约慢了 2 倍。

Source: I checked the source code of json.dump()and json.dumps()and also tested both the variants measuring the time with time.time()and watching the memory usage in htop.

来源:我检查了json.dump()和的源代码,并json.dumps()测试了两种变体,测量时间time.time()并观察 htop 中的内存使用情况。

回答by Antti Haapala

One notable difference in Python 2is that if you're using ensure_ascii=False, dumpwill properly write UTF-8 encoded data into the file (unless you used 8-bit strings with extended characters that are not UTF-8):

Python 2 中的一个显着区别是,如果您使用ensure_ascii=False,dump会将 UTF-8 编码数据正确写入文件(除非您使用带有非 UTF-8 扩展字符的 8 位字符串):

dumpson the other hand, with ensure_ascii=Falsecan produce a stror unicodejust depending on what types you used for strings:

dumps另一方面, withensure_ascii=False可以产生 astrunicode仅取决于您用于字符串的类型:

Serialize obj to a JSON formatted str using this conversion table. If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicodeinstance.

使用此转换表将 obj 序列化为 JSON 格式的 str 。如果 ensure_ascii 为 False,则结果可能包含非 ASCII 字符,返回值可能是一个unicode实例

(emphasis mine). Note that it may still be a strinstance as well.

(强调我的)。请注意,它也可能仍然是一个str实例。

Thus you cannot use its return value to save the structure into file without checking which format was returned and possibly playing with unicode.encode.

因此,如果不检查返回的格式并可能使用unicode.encode.

This of course is not valid concern in Python 3 any more, since there is no more this 8-bit/Unicode confusion.

这当然在 Python 3 中不再有效,因为不再存在这种 8 位/Unicode 混淆。



As for loadvs loads, loadconsiders the wholefile to be one JSON document, so you cannot use it to read multiple newline limited JSON documents from a single file.

对于loadvs loadsload整个文件视为一个 JSON 文档,因此您不能使用它从单个文件中读取多个换行限制的 JSON 文档。