JSON对象必须是str,而不是'bytes'

时间:2020-12-18 00:33:21

Using Python 3.5.1, I pulled in a text file where each line is in JSON form: {"a":"windows", "b":"stairs"...}

使用Python 3.5.1,我引入了一个文本文件,其中每一行都是JSON格式:{“a”:“windows”,“b”:“stairs”......}

import json
path = 'folder/data.txt'
records=[json.loads(line) for line in open(path,'rb')]

But I received the error:

但是我收到了错误:

the JSON object must be str, not 'bytes'

I have no problem printing the first line of file, so I am reassured that the file path is correct.

我打印第一行文件没有问题,所以我确信文件路径是正确的。

3 个解决方案

#1


3  

Open the file in text mode, not binary mode (possibly explicitly passing encoding='utf-8' to override the system default, since JSON is usually stored as UTF-8). The json module only takes str input; reading from a file opened in binary mode returns bytes objects:

以文本模式打开文件,而不是二进制模式(可能显式传递encoding ='utf-8'以覆盖系统默认值,因为JSON通常存储为UTF-8)。 json模块只接受str输入;从以二进制模式打开的文件读取将返回字节对象:

# Using with statement just for good form; in this case it would work the
# same on CPython, but on other interpreters or different CPython use cases,
# it's easy to screw something up; use with statements all the time to build good habits
with open(path, encoding='utf-8') as f:
    records=[json.loads(line) for line in f]

#2


2  

Try: records=[json.loads(line.decode()) for line in open(path,'rb')]

尝试:records = [json.loads(line.decode())for open(path,'rb')]

#3


1  

You do not want to specify "rb", since binary representation of the file isn't going to be readable by the JSON module. You likely want "utf-8" encoding and "read". EDIT: I originally had said both of these are defaults, but it was brought to my attention that many OS's have different default encoding and that Python uses the system setting as default in open(). I would therefore recommend explicitly providing the encoding setting as "utf-8".

您不希望指定“rb”,因为JSON模块无法读取该文件的二进制表示。你可能想要“utf-8”编码和“阅读”。编辑:我原本说过这两个都是默认值,但我注意到许多操作系统都有不同的默认编码,而且Python在open()中使用系统设置作为默认值。因此,我建议明确提供编码设置为“utf-8”。

json supports loading from an open file with "json.load" instead of "json.loads", which loads from a string, so we can skip the read-in-as-text and go right to JSON. I don't think you'll want to "loads" individual lines, since this likely won't be valid JSON.

json支持从打开的文件加载“json.load”而不是“json.loads”,它从字符串加载,因此我们可以跳过read-in-as-text并直接转到JSON。我认为你不想“加载”单独的行,因为这可能不是有效的JSON。

import json
# open has __enter__ and __exit__ functions, so we can call it as a guard
# using "with" syntax and it'll close when the scope ends
with open(r".\myjson.json", encoding="utf-8") as fh:
    # load() is a convenience function to help us avoid iterating lines
    # on our own. It calls loads() on the whole doc and returns an obj
    json_obj = json.load(fh)
print (json_obj)

#1


3  

Open the file in text mode, not binary mode (possibly explicitly passing encoding='utf-8' to override the system default, since JSON is usually stored as UTF-8). The json module only takes str input; reading from a file opened in binary mode returns bytes objects:

以文本模式打开文件,而不是二进制模式(可能显式传递encoding ='utf-8'以覆盖系统默认值,因为JSON通常存储为UTF-8)。 json模块只接受str输入;从以二进制模式打开的文件读取将返回字节对象:

# Using with statement just for good form; in this case it would work the
# same on CPython, but on other interpreters or different CPython use cases,
# it's easy to screw something up; use with statements all the time to build good habits
with open(path, encoding='utf-8') as f:
    records=[json.loads(line) for line in f]

#2


2  

Try: records=[json.loads(line.decode()) for line in open(path,'rb')]

尝试:records = [json.loads(line.decode())for open(path,'rb')]

#3


1  

You do not want to specify "rb", since binary representation of the file isn't going to be readable by the JSON module. You likely want "utf-8" encoding and "read". EDIT: I originally had said both of these are defaults, but it was brought to my attention that many OS's have different default encoding and that Python uses the system setting as default in open(). I would therefore recommend explicitly providing the encoding setting as "utf-8".

您不希望指定“rb”,因为JSON模块无法读取该文件的二进制表示。你可能想要“utf-8”编码和“阅读”。编辑:我原本说过这两个都是默认值,但我注意到许多操作系统都有不同的默认编码,而且Python在open()中使用系统设置作为默认值。因此,我建议明确提供编码设置为“utf-8”。

json supports loading from an open file with "json.load" instead of "json.loads", which loads from a string, so we can skip the read-in-as-text and go right to JSON. I don't think you'll want to "loads" individual lines, since this likely won't be valid JSON.

json支持从打开的文件加载“json.load”而不是“json.loads”,它从字符串加载,因此我们可以跳过read-in-as-text并直接转到JSON。我认为你不想“加载”单独的行,因为这可能不是有效的JSON。

import json
# open has __enter__ and __exit__ functions, so we can call it as a guard
# using "with" syntax and it'll close when the scope ends
with open(r".\myjson.json", encoding="utf-8") as fh:
    # load() is a convenience function to help us avoid iterating lines
    # on our own. It calls loads() on the whole doc and returns an obj
    json_obj = json.load(fh)
print (json_obj)