简体   繁体   English

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

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

I searched in this official document to find difference between the json.dump() and json.dumps() in python.我在这个官方文档中搜索以找到python中json.dump()和json.dumps()之间的区别。 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?但是它们之间的详细区别是什么?在什么情况下一个比其他更有优势?

There isn't much 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 with dump() .如果您想将 JSON 转储到文件/套接字或其他内容中,那么您应该使用dump() If you only need it as a string (for printing, parsing or whatever) then use dumps() (dump string)如果您只需要它作为字符串(用于打印、解析或其他),则使用dumps() (转储字符串)

As mentioned by Antti Haapala in this answer , there are some minor differences on the ensure_ascii behaviour.正如Antti Haapala 在这个答案中提到的,在ensure_ascii行为上有一些细微的差异。 This is mostly due to how the underlying write() function works, being that it operates on chunks rather than the whole string.这主要是由于底层write()函数的工作方式,因为它对块而不是整个字符串进行操作。 Check his answer for more details on that.检查他的回答以获取更多详细信息。

json.dump()

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object将 obj 作为 JSON 格式的流序列化为 fp(支持 .write() 的类文件对象

If ensure_ascii is False, some chunks written to fp may be unicode instances如果 ensure_ascii 为 False,则写入 fp 的某些块可能是 unicode 实例

json.dumps()

Serialize obj to a JSON formatted str将 obj 序列化为 JSON 格式的 str

If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance如果 ensure_ascii 为 False,则结果可能包含非 ASCII 字符,返回值可能是 unicode 实例

The functions with an s take string parameters.带有s的函数采用字符串参数。 The others take file streams.其他人采用文件流。

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.当您调用jsonstr = json.dumps(mydata)它首先在内存中创建数据的完整副本,然后才将它file.write(jsonstr)到磁盘。 So this is a faster method but can be a problem if you have a big piece of data to save.因此,这是一种更快的方法,但如果您要保存大量数据,则可能会出现问题。

When you call json.dump(mydata, file) -- without 's', new memory is not used, as the data is dumped by chunks.当您调用json.dump(mydata, file) -- 没有 's' 时,不会使用新内存,因为数据是按块转储的。 But the whole process is about 2 times slower.但整个过程大约慢了 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 中的内存使用情况的变体。

One notable difference in Python 2 is that if you're using ensure_ascii=False , dump will 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=Falsedump会将 UTF-8 编码数据正确写入文件(除非您使用带有非 UTF-8 扩展字符的 8 位字符串):

dumps on the other hand, with ensure_ascii=False can produce a str or unicode just depending on what types you used for strings:另一方面,根据您用于字符串的类型,使用ensure_ascii=False可以生成strunicode dumps

Serialize obj to a JSON formatted str using this conversion table.使用此转换表将 obj 序列化为 JSON 格式的 str 。 If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance .如果 ensure_ascii 为 False,则结果可能包含非 ASCII 字符,返回值可能是unicode实例

(emphasis mine). (强调我的)。 Note that it may still be a str instance 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 load vs loads , load considers the whole file to be one JSON document, so you cannot use it to read multiple newline limited JSON documents from a single file.至于loadloadsload整个文件视为一个 JSON 文档,因此您不能使用它从单个文件中读取多个换行限制的 JSON 文档。

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

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