简体   繁体   English

如何使用Python打开一个json.gz.part文件?

[英]How to open a json.gz.part file using Python?

I have lots of json.gz files in a directory and some them are json.gz.part.我在一个目录中有很多 json.gz 文件,其中一些是 json.gz.part。 Supposedly, when saving them, some of the files were too large and they were splitted.据说,在保存的时候,有些文件太大,被分割了。

I tried to open them as normally using:我试着像往常一样打开它们:

with gzip.open(file, 'r') as fin:
        json_bytes = fin.read()  
    json_str = json_bytes.decode('utf-8')            # 2. string (i.e. JSON)
    bb = json.loads(json_str)

But when it comes to the .gz.part files I get an error:但是当涉及到.gz.part文件时,我得到一个错误:

uncompress = self._decompressor.decompress(buf, size)

error: Error -3 while decompressing data: invalid code lengths set

I've tried the jiffyclub's solution, but I get the following error:我尝试了jiffyclub 的解决方案,但出现以下错误:

    _read_eof = gzip.GzipFile._read_eof

AttributeError: type object 'GzipFile' has no attribute '_read_eof'

EDIT:编辑:

If I read line by line I'm able to read most of the content file, until I get an error:如果我逐行阅读,我能够阅读大部分内容文件,直到出现错误:

with gzip.open(file2,'r') as fin:        
        for line in fin: 
            print(line.decode('utf-8'))

After printing most of the content I get:打印大部分内容后,我得到:

error: Error -3 while decompressing data: invalid code lengths set

But using this last method I cannot convert its content to a json file.但是使用最后一种方法我无法将其内容转换为 json 文件。

import gzip
import shutil

# open the .gz file
with gzip.open('file.gz.part', 'rb') as f_in:
    # open the decompressed file
    with open('file.part', 'wb') as f_out:
        # decompress the .gz file and write the decompressed data to the decompressed file
        shutil.copyfileobj(f_in, f_out)

# now you can open the decompressed file
with open('file.part', 'r') as f:
    # do something with the file
    contents = f.read()

This code will open the.gz.part file, decompress the data, and write the decompressed data to a new file called file.part.此代码将打开 .gz.part 文件,解压缩数据,并将解压缩的数据写入名为 file.part 的新文件。 You can then open the file.part file and read its contents just like you would with any other text file.然后您可以打开 file.part 文件并阅读其内容,就像阅读任何其他文本文件一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM