Python3 write gzip file - memoryview: a bytes-like object is required, not 'str'

后端 未结 4 1213
余生分开走
余生分开走 2020-12-16 13:33

I want to write a file. Based on the name of the file this may or may not be compressed with the gzip module. Here is my code:

import gzip
filen         


        
相关标签:
4条回答
  • 2020-12-16 14:14

    print is a relatively complex function. It writes str to a file but not the str that you pass, it writes the str that is the result of rendering the parameters.

    If you have bytes already, you can use fd.write(bytes) directly and take care of adding a newline if you need it.

    If you don't have bytes, make sure fd is opened to receive text.

    0 讨论(0)
  • 2020-12-16 14:27

    You can serialize it using pickle.

    First serializing the object to be written using pickle, then using gzip.

    To save the object:

    import gzip, pickle
    filename = 'non-serialize_object.zip'
    # serialize the object    
    serialized_obj = pickle.dumps(object)
    # writing zip file
    with gzip.open(filename, 'wb') as f:
       f.write(serialized_obj)
    

    To load the object:

    import gzip, pickle
    filename = 'non-serialize_object.zip'
    
    with gzip.open(filename, 'rb') as f:    
       serialized_obj = f.read()
    # de-serialize the object
    object = pickle.loads(serialized_obj)
    
    0 讨论(0)
  • 2020-12-16 14:29

    For me, changing the gzip flag to 'wt' did the job. I could write the original string, without "byting" it. (tested on python 3.5, 3.7 on ubuntu 16).

    From python 3 gzip doc - quoting: "... The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode..."

    import gzip
    
    filename = 'output.gz'
    opener = gzip.open if filename.endswith('.gz') else open
    with opener(filename, 'wt') as fd:
        print('blah blah blah', file=fd)
    
    !zcat output.gz
    > blah blah blah
    
    0 讨论(0)
  • 2020-12-16 14:37

    you can convert it to bytes like this.

    import gzip 
    with gzip.open(filename, 'wb') as fd:
       fd.write('blah blah blah'.encode('utf-8'))
    
    0 讨论(0)
提交回复
热议问题