admin管理员组文章数量:1221312
I currently have a zip file, that holds an underlying csv. I would like to read the file row by row without extracting the entire CSV file from the zip.
The underlying csv is simply too big to extract so I need a work around
I currently have a zip file, that holds an underlying csv. I would like to read the file row by row without extracting the entire CSV file from the zip.
The underlying csv is simply too big to extract so I need a work around
Share Improve this question asked Feb 7 at 15:05 polliewpolliew 1 New contributor polliew is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1 |1 Answer
Reset to default 2You can stream read the zip archive and get the contents of the first row via:
import zipfile
with zipfile.ZipFile("final_analysis_data.zip") as z: # 100m compressed
with z.open("final_analysis_data.csv") as f: # 650m uncompressed
first_row = next(f).decode()
input("check memory useage now, press enter to continue")
print(first_row)
The input()
statement will just pause and allow you to verify that you are not reading the entire archive into memory. With a 100m archive of a 650m csv in this example the python process uses 6m of ram.
Note:
If you feel that this resolves your issue, you might consider closing it as duplicate of:
Read a large zipped text file line by line in python
rather than accepting an answer.
本文标签: zipHow to read first row of a zipped csv in pythonStack Overflow
版权声明:本文标题:zip - How to read first row of a zipped csv in python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739312731a2157658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
zip
files are treated as folders, so you only have to supply the right path to your python program and it should be able to read it. – quamrana Commented Feb 7 at 15:08