Reading utf-8 characters from a gzip file in python

后端 未结 5 1137
执念已碎
执念已碎 2020-12-14 07:38

I am trying to read a gunzipped file (.gz) in python and am having some trouble.

I used the gzip module to read it but the file is encoded as a utf-8 text file so ev

5条回答
  •  无人及你
    2020-12-14 07:59

    In pythonic form (2.5 or greater)

    from __future__ import with_statement # for 2.5, does nothing in 2.6
    from gzip import open as gzopen
    
    with gzopen('foo.gz') as gzfile:
        for line in gzfile:
          print line.decode('utf-8')
    

提交回复
热议问题