How to compress or compact a string in Python

Go To StackoverFlow.com

0

I'm making a python "script" that sends a string to a webservice (in C#). I NEED to compress or compact this string, because the bandwidth and MBs data is LIMITED (yeah, in capitals because it's very limited).

I was thinking of converting it into a file and then compressing the file. But I'm looking for a method to directly compress the string.

How can I compress or compact the string?

2015-03-24 21:21
by Santiago Mendoza Ramirez
https://docs.python.org/3/library/archiving.htm - Colonel Thirty Two 2015-03-24 21:23
Will your web service understand your compressed string - Selcuk 2015-03-24 21:24
@Selcuk That's the idea. My webservice will descompress the string and store it in a database. If is important, the web service is in C# (.NET - Santiago Mendoza Ramirez 2015-03-24 21:25


6

How about zlib?

import zlib

a = "this string needs compressing"
a = zlib.compress(a)
print zlib.decompress(a) #outputs original contents of a

You can also use sys.getsizeof(obj) to see how much data an object takes up before and after compression.

2015-03-24 21:25
by hmir
Is there zlib avalaible in other lenguages? My web service is in C# (Sorry for not adding that information) - Santiago Mendoza Ramirez 2015-03-24 21:27
I found thishmir 2015-03-24 21:29
Ads