Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I need to read a file that is gzipped and lines are terminated by carriage returns. Using python 3.3 It seems you can just specify the newline variable in gzip.open. What would be the cleanest and still efficient way to do this in python 2.7.

like image 231
thekkid Avatar asked Apr 15 '14 18:04

thekkid


1 Answers

You could try io module to read the gzipped file as text line by line with universal newlines support:

import gzip
import io

with io.TextIOWrapper(io.BufferedReader(gzip.open(filename))) as file:
    for line in file:
        print line,
like image 170
jfs Avatar answered Oct 08 '22 08:10

jfs