MMF*_*MMF 119 python json python-2.7
在Python中,json.load()
和之间有什么区别json.loads()
?
我想load()函数必须与文件对象一起使用(我需要使用上下文管理器),而loads()函数将文件的路径作为字符串.这有点令人困惑.
字母" s " 是否json.loads()
代表字符串?
非常感谢你的回答!
Gij*_*ijs 115
是的,s
代表字符串.该json.loads
函数不采用文件路径,而是将文件内容作为字符串.请查看https://docs.python.org/2/library/json.html上的文档!
Rvd*_*vdK 28
文档非常清楚: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]]]]]]]])
Run Code Online (Sandbox Code Playgroud)
使用此转换表将fp(一个.read() - 支持包含JSON文档的类文件对象)反序列化为Python对象.
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Run Code Online (Sandbox Code Playgroud)
使用此转换表将s(包含JSON文档的str或unicode实例)反序列化为Python对象.
所以load
是一个文件,loads
一个string
Suf*_*ori 28
只是为每个人解释的内容添加一个简单的例子,
json.load()
json.load
可以反序列化文件本身,即它接受file
对象,
with open("json_data.json", "r") as content:
print(json.load(content))
Run Code Online (Sandbox Code Playgroud)
会输出,
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
Run Code Online (Sandbox Code Playgroud)
因为json.loads
是类型,json.loads()
即content.read()
如果我json.loads()
改用,
with open("json_data.json", "r") as content:
print(json.loads(content))
Run Code Online (Sandbox Code Playgroud)
我会收到这个错误:
TypeError:期望的字符串或缓冲区
json.loads()
content.read()
deserailize字符串.
使用<type 'str'>
与json.load()
该文件的内容的回报,
with open("json_data.json", "r") as content:
print(json.loads(content.read()))
Run Code Online (Sandbox Code Playgroud)
输出,
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
Run Code Online (Sandbox Code Playgroud)
那是因为类型content.read()
是字符串,即json.load
如果我用json.loads
用sys.stdin
,我得到的错误,
with open("json_data.json", "r") as content:
print(json.load(content.read()))
Run Code Online (Sandbox Code Playgroud)
给人,
AttributeError:'str'对象没有属性'read'
所以,现在您知道file
反序列化文件并print(json.load(sys.stdin))
反序列化字符串.
另一个例子,
json.loads()
返回print(json.loads(sys.stdin.read()))
对象,所以如果我这样做json.load
,我将获得实际的json数据,
cat json_data.json | ./test.py
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
Run Code Online (Sandbox Code Playgroud)
如果我想使用file
,我会json.loads
改为.
Wla*_*lad 21
json.load() 需要一个FILE
json.load() 需要一个文件(文件对象)——例如一个你之前打开的文件,如
'files/example.json'
.
json.loads() 需要一个字符串
json.loads() 需要一个(有效的)JSON 字符串 - 即
{"foo": "bar"}
假设您有一个包含以下内容的文件example.json:{ "key_1": 1, "key_2": "foo", "Key_3": null }
>>> import json
>>> file = open("example.json")
>>> type(file)
<class '_io.TextIOWrapper'>
>>> file
<_io.TextIOWrapper name='example.json' mode='r' encoding='UTF-8'>
>>> json.load(file)
{'key_1': 1, 'key_2': 'foo', 'Key_3': None}
>>> json.loads(file)
Traceback (most recent call last):
File "/usr/local/python/Versions/3.7/lib/python3.7/json/__init__.py", line 341, in loads
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper
>>> string = '{"foo": "bar"}'
>>> type(string)
<class 'str'>
>>> string
'{"foo": "bar"}'
>>> json.loads(string)
{'foo': 'bar'}
>>> json.load(string)
Traceback (most recent call last):
File "/usr/local/python/Versions/3.7/lib/python3.7/json/__init__.py", line 293, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)