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

前端 未结 4 1171
遥遥无期
遥遥无期 2020-12-04 08:15

I searched in this official 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

4条回答
  •  再見小時候
    2020-12-04 08:39

    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(). If you only need it as a string (for printing, parsing or whatever) then use dumps() (dump string)

    As mentioned by Antti Haapala in this answer, there are some minor differences on the ensure_ascii behaviour. This is mostly due to how the underlying write() function works, being that it operates on chunks rather than the whole string. 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

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

    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

提交回复
热议问题