Compression in Python Programming Tutorial

Compression in Python Programming Tutorial

20 thoughts on “Compression in Python Programming Tutorial”

  1. i have a question. Do you know how to create a compressed file. For example in my class we have to get a file that has a text and get the frequency of each word and so and then compress the file. I was wondering if you could help me. i  am just so lost.

  2. The downside to using base64 output for the compression is that you don't get quite as good a compression ratio.  To avoid using it – the file object used for writing the compressed data needs to be set to 'wb'  (binary write), and 'rb' (binary read) when decompressing.   In my testing:  my decompressed file size was ~130 KB,  base64 encoded compressed file size was ~80 KB, whereas in binary format, the compressed file size was ~60 KB.  Examples:

    WRITING DATA

    plainData = open("decompressed.txt","r").read()
    compFile = open("compressed.txt","wb")
    compFile.write(zlib.compress(plainData)
    compFile.close()

    READING DATA

    compressedData = open("compressed.txt","rb").read()
    plainData = zlib.decompress(compressedData)

    Hope that helps  🙂

  3. Please could you help me, I get the following error:

    Compression: 

    Raw Size:  104270
    Traceback (most recent call last):
      File "C:/Users/Emma/Desktop/Compression Algorithm.py", line 25, in <module>
        end()
      File "C:/Users/Emma/Desktop/Compression Algorithm.py", line 23, in end
        main()
      File "C:/Users/Emma/Desktop/Compression Algorithm.py", line 8, in main
        compressed = zlib.compress(text, 9)
    TypeError: 'str' does not support the buffer interface
    >>> 

    For this code:

    import time, base64, sys, random, winsound, doctest, urllib, math, zlib; from tkinter import *
    #start

    def main():
        text = open("sample.txt", "r").read()
        print("Raw Size: ", sys.getsizeof(text))

        compressed = zlib.compress(text, 9)
        print("9 Compressed size: ", sys.getsizeof(compressed))

        savecomp = open("compdata.txt", "a")
        savecomp.write(compressed)
        savecomp.close()
        print(compressed)

        decompressed = zlib.decompress(compressed)
        print(decompressed)

    def end():
        print("Compression: ")
        print()
        time.sleep(1)
        main()

    end()
    #end

  4. I would like to learn how to write my own algorithm for compressing files, but I don't know what technologies to learn. In simpler terms what do I have to learn to write my own compressing algorithms like zlib, if that makes sense? Like how do I create another version of zlib?

  5. Could you please help me with this error, when I try and run your code it does this.compression:import zlib,sys,time,base64text = open('file.txt','w').read()
    print 'raw size:' ,sys.getsizeof(text)compressed = zlib.compress(text,9)
    print '9 compressed siz:',sys.getsizeof(compressed)savecomp = open('compdata.txt','a')
    savecomp.write(compressed)
    savecomp.close()that is the code by it says syntax error on the  2nd apostrophe on the line print 'raw size:' ,sys.getsizeof(text)

  6. Please could you help me I get the following error:
    Traceback (most recent call last):
    File "C:UsersPrashanth GMAppDataLocalProgramsPythonPython36comp.py", line 7, in <module>
    compressed = base64.b64encode(zlib.compress(text))
    TypeError: a bytes-like object is required, not 'str'

    I am Using Python 3.6.0 Shell

    My Code is

    import zlib
    import sys
    import time
    import base64

    text= "compression example with encoding"
    compressed = base64.b64encode(zlib.compress(text))
    print (compressed)

    output = open("encodecomp.txt","a")
    output.write(compressed)
    output.close()

    readFile = open("encodecomp.txt","r").read()

    decompressed = zlib.decompress(base64.b64decode(readFile))

  7. Can we compress a file then compress the compressed and so on…?
    Also when I tried to compress a sting I got an error who said that I can compress only bytes-like objects.What it is going on?

  8. If anybody is getting the error message: TypeError: a bytes-like object is required, not 'str'

    You need to convert the text file into bytes:

    text_string = open('compress_me.txt','r').read()

    text_bytes = text_string.encode("utf-8")

    compressed = zlib.compress(text_bytes,9)

    print("9 compressed size:",sys.getsizeof(compressed))

  9. hey dude, thanks for your tutorial video.
    but i have a question, when i try compressing and decompressing a file without base64, its work finely. and also when i use base64 it work finely too. do u have opinion about this ??

  10. Here because I for some reason decided to run a website that archives and provides an API for full historical WoW auction house snapshots across all regions. Realized I probably bit off more than I could chew when I gathered some data for sampling and realized that storing it as received would generate about 1.6 TB of data monthly xD
    Simply restructuring the data got me to about 10% of that. Pushing the result through 7zip on Ultra nets me 20% of that , so I'm left with something that's 2% the original size, which is pretty impressive. Will be pretty interesting to see how Python libraries compare to 7zip.
    Anyways, compression = epicly useful stuff.

    UPDATE: Well, after some testing, zlib underperformed 7zip (was getting about 3.5% overall). The algorithm I used with 7zip was LZMA, so I did a bit of searching, and sure enough that also ships with Python, and got me the same 2% overall compression I had with 7zip. So check that one out if you really need to squeeze out every last byte out of that data. Documentation here: https://docs.python.org/dev/library/lzma.html

Leave a Reply

Your email address will not be published. Required fields are marked *