简体   繁体   English

你如何使用python 2.7迭代一个gzipped回车文件?

[英]How do you iterate through a gzipped carriage-return file using python 2.7?

I have to use python 2.7 because I'm using the boto library and boto3 is experimental. 我必须使用python 2.7,因为我使用的是boto库,而boto3是实验性的。 I need to read a file that is gzipped and lines are terminated by carriage returns. 我需要读取一个gzip压缩的文件,并通过回车符终止行。 Using python 3.3 It seems you can just specify the newline variable in gzip.open. 使用python 3.3看来你可以在gzip.open中指定换行变量。 What would be the cleanest and still efficient way to do this in python 2.7. 在python 2.7中这样做最干净,最有效的方法是什么。

You could try io module to read the gzipped file as text line by line with universal newlines support: 您可以尝试使用io模块将gzip压缩文件作为文本逐行读取,并支持通用换行符:

import gzip
import io

with io.TextIOWrapper(io.BufferedReader(gzip.open(filename))) as file:
    for line in file:
        print line,

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

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