Ijson 从列表中解析
Ijson parse from list
我有一个列表,其中每个项目都包含 JSON 数据,因此我尝试使用 Ijson
解析数据,因为数据负载会很大。
这就是我想要实现的目标:
article_data=#variable which contains the list
parser = ijson.parse(article_data)
for id in ijson.items(parser, 'item'):
if(id['article_type'] != "Monthly Briefing" and id['article_type']!="Conference"):
data_article_id.append(id['article_id'])
data_article_short_desc.append(id['short_desc'])
data_article_long_desc.append(id['long_desc'])
这是我得到的错误:
AttributeError: 'generator' object has no attribute 'read'
我想将 list
转换为 string
,然后尝试在 Ijson
中解析,但它失败了,并给出了同样的错误。
有什么建议吗?
data_article_id=[]
data_article_short_desc=[]
data_article_long_desc=[]
for index in article_data:
parser = ijson.parse(index)
for id in ijson.items(parser, 'item'):
if(id['article_type'] != "Monthly Briefing" and id['article_type']!="Conference"):
data_article_id.append(id['article_id'])
data_article_short_desc.append(id['short_desc'])
data_article_long_desc.append(id['long_desc'])
因为它在列表中,我也尝试了这个..但它给了我同样的错误。
'generator'对象没有属性'read'
我假设您有一个要解析的字节字符串 json 对象列表。
ijson.items(JSON, prefix) 将可读字节对象作为输入。也就是说,它需要一个打开的文件或类似文件的对象作为输入。具体来说,输入应该是字节类文件对象。
如果你正在使用 Python 3,你可以使用 io
模块
io.BytesIO 创建内存中的二进制流。
示例
假设输入是 [b'{"id": "ab"}', b'{"id": "cd"}']
list_json = [b'{"id": "ab"}', b'{"id": "cd"}']
for json in list_json:
item = ijson.items(io.BytesIO(json), "")
for i in item:
print(i['id'])
Output:
ab
cd
我有一个列表,其中每个项目都包含 JSON 数据,因此我尝试使用 Ijson
解析数据,因为数据负载会很大。
这就是我想要实现的目标:
article_data=#variable which contains the list
parser = ijson.parse(article_data)
for id in ijson.items(parser, 'item'):
if(id['article_type'] != "Monthly Briefing" and id['article_type']!="Conference"):
data_article_id.append(id['article_id'])
data_article_short_desc.append(id['short_desc'])
data_article_long_desc.append(id['long_desc'])
这是我得到的错误:
AttributeError: 'generator' object has no attribute 'read'
我想将 list
转换为 string
,然后尝试在 Ijson
中解析,但它失败了,并给出了同样的错误。
有什么建议吗?
data_article_id=[]
data_article_short_desc=[]
data_article_long_desc=[]
for index in article_data:
parser = ijson.parse(index)
for id in ijson.items(parser, 'item'):
if(id['article_type'] != "Monthly Briefing" and id['article_type']!="Conference"):
data_article_id.append(id['article_id'])
data_article_short_desc.append(id['short_desc'])
data_article_long_desc.append(id['long_desc'])
因为它在列表中,我也尝试了这个..但它给了我同样的错误。
'generator'对象没有属性'read'
我假设您有一个要解析的字节字符串 json 对象列表。
ijson.items(JSON, prefix) 将可读字节对象作为输入。也就是说,它需要一个打开的文件或类似文件的对象作为输入。具体来说,输入应该是字节类文件对象。
如果你正在使用 Python 3,你可以使用 io
模块
io.BytesIO 创建内存中的二进制流。
示例
假设输入是 [b'{"id": "ab"}', b'{"id": "cd"}']
list_json = [b'{"id": "ab"}', b'{"id": "cd"}']
for json in list_json:
item = ijson.items(io.BytesIO(json), "")
for i in item:
print(i['id'])
Output:
ab
cd