In Python, what is the difference between json.load() and json.loads()?
在Python中,json.load()和json.load()有什么区别?
I guess that the load() function must be used with a file object (I need thus to use a context manager) while the loads() function take the path to the file as a string. It is a bit confusing.
我猜想load()函数必须与一个文件对象一起使用(因此我需要使用一个上下文管理器),而load()函数以字符串的形式获取文件的路径。这有点令人困惑。
Does the letter "s" in json.loads() stand for string?
在json.load()中的字母“s”表示字符串吗?
Thanks a lot for your answers!
非常感谢你的回答!
2 个解决方案
#1
43
Yes, it stands for string. The json.loads
function does not take the file path, but the file contents as a string. Look at the documentation at https://docs.python.org/2/library/json.html!
是的,它代表字符串。json。load函数不接受文件路径,而是将文件内容作为字符串。查看https://docs.python.org/2/library/json.html的文档!
#2
19
Documentation is quite clear: https://docs.python.org/2/library/json.html
文档非常清晰:https://docs.python.org/2/library/json.html
json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.
使用此转换表将fp(一个.read()—支持包含JSON文档的类文件对象)反序列化为Python对象。
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.
使用此转换表将s(包含JSON文档的str或unicode实例)反序列化为Python对象。
So load
is for a file, loads
for a string
load表示文件,load表示字符串
#1
43
Yes, it stands for string. The json.loads
function does not take the file path, but the file contents as a string. Look at the documentation at https://docs.python.org/2/library/json.html!
是的,它代表字符串。json。load函数不接受文件路径,而是将文件内容作为字符串。查看https://docs.python.org/2/library/json.html的文档!
#2
19
Documentation is quite clear: https://docs.python.org/2/library/json.html
文档非常清晰:https://docs.python.org/2/library/json.html
json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.
使用此转换表将fp(一个.read()—支持包含JSON文档的类文件对象)反序列化为Python对象。
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.
使用此转换表将s(包含JSON文档的str或unicode实例)反序列化为Python对象。
So load
is for a file, loads
for a string
load表示文件,load表示字符串