Python: Brotli compression

In this tutorial we will check how to compress a string using the Brotli compression algorithm.


In this tutorial we will check how to compress a string using the Brotli compression algorithm. You can check the full specification of the algorithm here.

We will be using this library, which includes a Python module. As explained in the  Python section of the GitHub page of the library, you can install it using pip with the following command:

pip install brotli

As we will be seeing below, the code for this tutorial will be really simply and short, since this module offers a function that receives a string and returns as output the compressed content, without the need for any additional procedures.

This tutorial was tested on Python version 2.7.8.

The code

We will start the code by importing the previously installed brotli module. This module will make available the functionalities we need to perform the compression.

import brotli

Then, in order to perform the compression, we simply need to call the compress function of the brotli module, passing as input the string we want to compress.

Note that this function has four more optional parameters, as can be seen here. Nonetheless, those for parameters have default values that we will keep unchanged.

For testing purposes, we will pass as input the string “hello world!” repeated one hundred times. We can repeat a string in Python by using the * operator after the string, followed by the number of times we want it to be repeated.

We will store the result of the compression in a variable, as shown below.

compressed = brotli.compress("hello world!" * 100)

The result from the compress function call will be a string (you can confirm it by calling the type operator on the variable).

So, we will iterate all characters of that string and obtain the corresponding byte value. We can obtain each individual character by using a for in loop.

For each iteration, we will use the ord function to obtain the value of the byte that represents the corresponding character.

You can check the loop and the printing of the values below. Note that the comma at the end of the print function call is a trick to avoid inserting a new line at the end of the printed content. This way, we print all the bytes in the same line.

for byte in compressed:
    print ord(byte),

The final complete code can be seen below.

import brotli

compressed = brotli.compress("hello world!" * 100)

for byte in compressed:
    print ord(byte),

Testing the code

To test the code, simply run it in a Python tool of your choice. I’ll be using IDLE, a Python IDE. You should get an output similar to figure 1, which shows the bytes of the compressed string.

Brotli compression using Python

Figure 1 -Bytes of the compressed content.

2 Replies to “Python: Brotli compression”

Leave a Reply