Read a large zipped text file line by line in python

后端 未结 1 704
再見小時候
再見小時候 2021-01-03 21:50

I am trying to use zipfile module to read a file in an archive. the uncompressed file is ~3GB and the compressed file is 200MB. I don\'t want them in memory as I process the

相关标签:
1条回答
  • 2021-01-03 22:19

    Python file objects provide iterators, which will read line by line. file.readlines() reads them all and returns a list - which means it needs to read everything into memory. The better approach (which should always be preferred over readlines()) is to just loop over the object itself, E.g:

    import zipfile
    with zipfile.ZipFile(...) as z:
        with z.open(...) as f:
            for line in f:
                print line
    

    Note my use of the with statement - file objects are context managers, and the with statement lets us easily write readable code that ensures files are closed when the block is exited (even upon exceptions). This, again, should always be used when dealing with files.

    0 讨论(0)
提交回复
热议问题