如何使用不同的代码块读取gzip JSON文件中的所有数据

时间:2018-09-17 05:56:30

标签: json python-3.x file

我已经压缩了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)

但是上面的代码无法读取此文件。 如何读取此文件中的所有数据?我可以提出建议吗?

1 个答案:

答案 0 :(得分:0)

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

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