According to this website and guide it should be pretty trivial to save the string as compressed using zlib.
import zlib
my_data = 'Hello world'
compressed_data = zlib.compress(my_data, 2)
f = open('outfile.txt', 'w')
f.write(compressed_data)
f.close()
This code should save "Hello world" into txt file as compressed. I'm getting these errors:
compressed_data = zlib.compress(my_data, 2)
TypeError: a bytes-like object is required, not 'str'
I have also tried adding this but then i get other error saying
new_data = zlib.compress(my_data.encode())
compressed_data = zlib.compress(new_data, 2)
And then i get errors like this:
TypeError: write() argument must be str, not bytes
I have also tried adding b' in front of the text in my_data but that gave me new error
my_data = b'Hello world'
TypeError: write() argument must be str, not bytes