簡體   English   中英

Python:如何在Python中從壓縮的7z文件讀取一行?

[英]Python: How can I read a line from a compressed 7z file in Python?

我有一個包含一個xml文件的多GB 7z存檔。 我想一次從此壓縮文件中讀取一行,直到在Python 3.4上達到文件(文件的) EOF為止。 我無法將其解壓縮為完整大小,大約是TB。

建議我使用許多庫,例如pylzmalzma但它們不支持7z格式。 libarchive確實支持7z,但它讀取的是塊,我認為不一定是文件中的文本行。

請提供建議。 謝謝。

(在yield部分進行詳細說明)注意,我不知道此lib或用於獲取未壓縮數據塊的函數。 但我的意思是這樣的:

def 7zreadline(filename):
    with open(filename, 'rb') as fh: #automatically closes filehandler when finished
        archive = py7zlib.Archive7z(fh)
        current_line = ''
        for block in archive.getblock(): #I do not know how you get a block of uncompressed data, so I ''abstract'' the call, you get the idea...
            current_line += block
            while '\n' in current_line:
                yield current_line[:current_line.index('\n')+1] # gives all until '\n' to the caller
                current_line = current_line[current_line.index('\n')+1:] # now, initialize current_line with the rest of your block.
        yield current_line #return the end of file

然后,您可以像這樣使用它:

for line in 7zreadline('myfile.zip'):
    print(line)

如果知道圖書館的人可以得到正確的信息,則歡迎進行編輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM