如何在Python中压缩或压缩字符串 [英] How to compress or compact a string in Python

查看:444
本文介绍了如何在Python中压缩或压缩字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个python脚本,该脚本将字符串发送到Web服务(使用C#)。我需要压缩或压缩该字符串,因为带宽和MBs数据是有限的(是的,因为它非常有限,所以用大写字母表示)。

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?

推荐答案

zlib 怎么样?

import zlib

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

您可以还可以使用 sys.getsizeof(obj)来查看对象在压缩前后需要占用多少数据。

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

这篇关于如何在Python中压缩或压缩字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆