How to compress or compact a string in Python

后端 未结 2 615
暗喜
暗喜 2021-02-08 01:42

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 c

2条回答
  •  天涯浪人
    2021-02-08 01:52

    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.

提交回复
热议问题