python import gzip use analytic

The second parameter is a function of the python open open mode, such as rb, rt, which consists of two parts, the first letter indicates the mode open in read-only (r = read), the second letter indicates the content identification data into what . b indicates binary data is 2, t represents text data. t is the default parameter is not specified is t, so you open mode is rt, read-only text mode. Text mode generally need to specify the encoding. Incoming encoding = encoding. The sentence changed

Using gzip compression module to complete the file.

import gzip

f_in = open ( "data.txt", "rb") # Open File

f_out = gzip.open ( "data.txt.gz", "wb") # create a compressed file object

f_out.writelines(f_in)

f_out.close()

f_in.close()

Using gzip module to complete decompression of files.

import gzip

f = gzip.open ( "data.txt.gz", 'rb') # open a compressed file object

f_out = open ( "data.txt", "w") # Open the unpacked files saved content

file_content = f.read () # read the contents of the file after extraction

f_out.write (file_content.decode ( "utf-8")) # write a new file which

print (file_content) # print read content

f.close () # close the file stream

f_out.close()

Published 12 original articles · won praise 3 · Views 210

Guess you like

Origin blog.csdn.net/weixin_45426801/article/details/104051467