gpt4 book ai didi

python - 加载存储在 BytesIO 对象中的 json 文件的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-04 16:29:36 24 4
gpt4 key购买 nike

我收到的数据是 bytes因此我需要类似文件的临时容器。据我所知 BytesIO是类文件对象,但 json.load()对它不起作用:

In [1]: import json
...: from io import BytesIO, TextIOWrapper

In [2]: d, b = dict(a=1, b=2), BytesIO()

In [3]: b.write(json.dumps(d).encode())
Out[3]: 16

In [4]: b.seek(0)
Out[4]: 0

In [5]: b.read()
Out[5]: b'{"a": 1, "b": 2}'

In [6]: b.seek(0)
Out[6]: 0

In [7]: json.load(b)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-233ac51d2711> in <module>()
----> 1 json.load(b)

/usr/lib/python3.5/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
266 cls=cls, object_hook=object_hook,
267 parse_float=parse_float, parse_int=parse_int,
--> 268 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
269
270

/usr/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
310 if not isinstance(s, str):
311 raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312 s.__class__.__name__))
313 if s.startswith(u'\ufeff'):
314 raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",

TypeError: the JSON object must be str, not 'bytes'

一种有效的方法:
In [8]: json.loads(b.getvalue().decode())
Out[8]: {'a': 1, 'b': 2}

另一个,大概更有效?
In [10]: b.seek(0)
Out[10]: 0

In [11]: json.load(TextIOWrapper(b, encoding='utf-8'))
Out[11]: {'a': 1, 'b': 2}

我有更多(更好)的选择吗?如果否,应首选上述哪一种方法?

最佳答案

如果您使用的是 Python 3.5,请升级到 3.6+

3.5

>>> import sys                                                                                                   

>>> sys.version
'3.5.0 (default, Feb 16 2017, 15:47:16) \n[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]'


>>> import json

>>> from io import BytesIO

>>> d, b = dict(a=1, b=2), BytesIO()

>>> b.write(json.dumps(d).encode())
16


>>> b.seek(0)
0


>>> json.load(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/cmermingas/.pyenv/versions/3.5.0/lib/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/cmermingas/.pyenv/versions/3.5.0/lib/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

the JSON object must be str, not 'bytes'

3.6
>>> import sys

>>> sys.version
'3.6.0 (default, Jul 10 2017, 22:19:26) \n[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)]'

>>> import json

>>> from io import BytesIO

>>> d, b = dict(a=1, b=2), BytesIO()

>>> b.write(json.dumps(d).encode())
16

>>> b.seek(0)
0

>>> json.load(b)
{'a': 1, 'b': 2}

关于python - 加载存储在 BytesIO 对象中的 json 文件的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53118672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com