【问题标题】:read a file with a stream of json objects in python在python中读取带有json对象流的文件
【发布时间】:2019-07-18 02:15:04
【问题描述】:

这可能是多余的,但是在阅读了以前的帖子和答案之后,我仍然没有让我的代码工作。我有一个非常大的文件,其中包含多个未由任何值分隔的 json 对象:

{“_index”:“1234”,“_type”:“11”,“_id”:“1234”,“_score”:0.0,“fields”:{“c_u”:[“url.com”] ,“tawgs.id”:[“p6427”]}}{“_index”:“1234”,“_type”:“11”,“_id”:“786fd4ad2415aa7b”,“_score”:0.0,“fields”:{ “c_u”:[“url2.com”],“tawgs.id”:[“p12519”]}}{“_index”:“1234”,“_type”:“11”,“_id”:“5826e7cbd92d951a”, “_score”:0.0,“字段”:{“tawgs.id”:[“p8453”,“p8458”]}}

我已经读到这正是 JSON-RPC 应该看起来的样子,但仍然无法实现打开/解析文件以在 python 中创建数据帧。

我尝试了以下格式:

i = 0
d = json.JSONDecoder()
while True:
    try:
        obj, i = d.raw_decode(s, i)
    except ValueError:
        return
    yield obj

但它没有用。

我也试过基本的:

with open('output.json','r') as f:
    data = json.load(f)

但我抛出了错误:

JSONDecodeError:额外数据:第 1 行第 184 列(字符 183)

用 append 尝试 json.decode() 也不起作用,返回的数据为空 []

data = []
with open('es-output.json', 'r') as f:
    for line in f:
        try:
            data.append(json.loads(line))
        except json.decoder.JSONDecodeError:
            pass # skip this line 

请帮忙!提前致谢

【问题讨论】:

标签: python json python-3.x


【解决方案1】:

这将尝试迭代解码 s 内的 JSON 流:

s = '''{"_index": "1234", "_type": "11", "_id": "1234", "_score": 0.0, "fields": {"c_u": ["url.com"], "tawgs.id": ["p6427"]}}{"_index": "1234", "_type": "11", "_id": "786fd4ad2415aa7b", "_score": 0.0, "fields": {"c_u": ["url2.com"], "tawgs.id": ["p12519"]}}{"_index": "1234", "_type": "11", "_id": "5826e7cbd92d951a", "_score": 0.0, "fields": {"tawgs.id": ["p8453", "p8458"]}}'''

import json

d = json.JSONDecoder()

idx = 0
while True:
    if idx >= len(s):
        break
    data, i = d.raw_decode(s[idx:])
    idx += i
    print(data)
    print('*' * 80)

打印:

{'_index': '1234', '_type': '11', '_id': '1234', '_score': 0.0, 'fields': {'c_u': ['url.com'], 'tawgs.id': ['p6427']}}
********************************************************************************
{'_index': '1234', '_type': '11', '_id': '786fd4ad2415aa7b', '_score': 0.0, 'fields': {'c_u': ['url2.com'], 'tawgs.id': ['p12519']}}
********************************************************************************
{'_index': '1234', '_type': '11', '_id': '5826e7cbd92d951a', '_score': 0.0, 'fields': {'tawgs.id': ['p8453', 'p8458']}}
********************************************************************************

【讨论】:

  • 所以如果我的“s”值是一个不是字符串对象的 json 文件,因为我使用 json.dump() 来编写文件,我最初将如何将我的文件转换为 json字符串类型?当我尝试使用 json.dumps() 写入文件时,我得到一个空集
  • @aesthetics 只需在s:s = open('your_file.txt', 'r').read()中加载带有JSON对象的文件内容
  • @aesthetics 我对弹性搜索没有任何经验,但是您要么有一些包含 JSON 值的字符串,要么需要从文件中加载此字符串。
  • 作为下一步,只是为了澄清,如果类型是字符串,是否有办法将 json 展平以将其放入数据框?
  • @aesthetics 这是 Panda/Numpy 专家的问题,但我敢打赌,有一些方法可以直接从 json 加载数据。您可以提出其他问题,这些 cmets 不适合它。
【解决方案2】:

问题出在数据本身! 在此数据中,您使用 3 个值但没有键。

第一个是:

{"_index".... ["p6427"]}}

第二个是:

{"_index".... ["p12519"]}}

第三个是:

{"_index".... ["p8458"]}}

你宁愿影响每个值一个键,例如:

{
"k1":{"_index": "1234", "_type": "11", "_id": "1234", "_score": 0.0, "fields": {"c_u": ["url.com"], "tawgs.id": ["p6427"]}},

"k2":{"_index": "1234", "_type": "11", "_id": "786fd4ad2415aa7b", "_score": 0.0, "fields": {"c_u": ["url2.com"], "tawgs.id": ["p12519"]}},

"k3":{"_index": "11_20190714_184325_01", "_type": "11", "_id": "5826e7cbd92d951a", "_score": 0.0, "fields": {"tawgs.id": ["p8453", "p8458"]}}
}

这样一切都会正常运行,数据也会被很好地加载。

【讨论】:

  • 嗯,我正在从 elasticsearch-py 中提取数据,所以我不确定如何操作和引入密钥?集成python和elasticsearch也很新手:/
  • 尝试找出一些独特的特征来创建密钥!
  • 如果我无法初步读取/打开包含数据的文件,如何在 json 中创建密钥?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2021-08-07
  • 1970-01-01
  • 2021-03-03
  • 2018-01-14
相关资源
最近更新 更多