对Python中的gzip压缩文件使用csvreader [英] Using csvreader against a gzipped file in Python

查看:140
本文介绍了对Python中的gzip压缩文件使用csvreader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多压缩的CSV文件,我想使用Python的内置CSV阅读器打开这些文件进行检查.我想这样做,而不必先手动将它们解压缩到磁盘上.我想我想以某种方式获取未压缩数据的流,并将其传递到CSV阅读器.在Python中有可能吗?

I have a bunch of gzipped CSV files that I'd like to open for inspection using Python's built in CSV reader. I'd like to do this without having first to manually unzip them to disk. I guess I want to somehow get a stream to the uncompressed data, and pass this into the CSV reader. Is this possible in Python?

推荐答案

使用gzip模块:

with gzip.open(filename, mode='rt') as f:
    reader = csv.reader(f)
    #...

这篇关于对Python中的gzip压缩文件使用csvreader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆