【问题标题】:How to read all the data in gzipped JSON file with chunks of data different codes如何使用不同代码的数据块读取 gzip 压缩的 JSON 文件中的所有数据
【发布时间】:2019-02-21 00:13:37
【问题描述】:

我已压缩 JSON 文件,从 Auth0 导出,内容如下:

{"Id":"auth0|59bdb71ea714e32e8a6662fd","Nickname":"autoqa.krd0xj","Name":"autoqa.krd0xj@marketingg2.com","Email":"autoqa.krd0xj@marketingg2.com","Email Verified":false,"Connection":"Username-Password-Authentication","Created At":"2017-09-16T23:43:27.002Z","Updated At":"2017-09-16T23:43:27.490Z"},
{"Id":"auth0|18142559","Nickname":"moharvey","Name":"moharvey@ymail.com","Email":"moharvey@ymail.com","Connection":"Username-Password-Authentication","Created At":"2017-08-31T18:55:02.688Z","Updated At":"2017-08-31T19:01:36.994Z"}

我尝试使用此代码:

import json  
import gzip
with gzip.GzipFile("file.gz", 'r') as fin:   
    json_bytes = fin.read()                      
json_str = json_bytes.decode('utf-8')            
data = json.loads(json_str)                      
print(data)

但是上面的代码不能读取这个文件。 如何读取此文件中的所有数据?可以给点建议吗?

【问题讨论】:

    标签: json python-3.x file


    【解决方案1】:

    我刚刚找到了解决方案。我打算删除这个问题,但我没有看到任何类似的问题。

    table=[]
    with gzip.GzipFile("file.gz", 'r') as fin:   
        for line in fin:
            table.append(json.loads(line))
    for row in table:
        print(row)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 2020-11-03
      • 1970-01-01
      • 2016-09-23
      • 2014-07-04
      相关资源
      最近更新 更多