Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的基本lzw压缩帮助_Python_Encode_Lzw - Fatal编程技术网

python中的基本lzw压缩帮助

python中的基本lzw压缩帮助,python,encode,lzw,Python,Encode,Lzw,我正试图编写一个非常基本的脚本,它将接收一些输入文本,并使用lzw压缩它,使用以下软件包: 我以前从未尝试过使用python进行任何编码,对此我感到非常困惑=(-除了包信息之外,我在网上也找不到任何关于它的文档 以下是我所拥有的: import lzw file = lzw.readbytes("collectemailinfo.txt", buffersize=1024) enc = lzw.compress(file) print enc 任何形式的帮助或指点都将不胜感激 谢谢=)以下是

我正试图编写一个非常基本的脚本,它将接收一些输入文本,并使用lzw压缩它,使用以下软件包:

我以前从未尝试过使用python进行任何编码,对此我感到非常困惑=(-除了包信息之外,我在网上也找不到任何关于它的文档

以下是我所拥有的:

import lzw

file = lzw.readbytes("collectemailinfo.txt", buffersize=1024)
enc = lzw.compress(file)
print enc
任何形式的帮助或指点都将不胜感激


谢谢=)

以下是软件包API:

您可以读取压缩和解压缩的psuedo代码

你还有什么不明白的吗

以下是一个例子:

蟒蛇

在此版本中,DICT包含混合类型的数据:

def compress(uncompressed):
    """Compress a string to a list of output symbols."""

    # Build the dictionary.
    dict_size = 256
    dictionary = dict((chr(i), chr(i)) for i in xrange(dict_size))
    # in Python 3: dictionary = {chr(i): chr(i) for i in range(dict_size)}

    w = ""
    result = []
    for c in uncompressed:
        wc = w + c
        if wc in dictionary:
            w = wc
        else:
            result.append(dictionary[w])
            # Add wc to the dictionary.
            dictionary[wc] = dict_size
            dict_size += 1
            w = c

    # Output the code for w.
    if w:
        result.append(dictionary[w])
    return result



def decompress(compressed):
    """Decompress a list of output ks to a string."""

    # Build the dictionary.
    dict_size = 256
    dictionary = dict((chr(i), chr(i)) for i in xrange(dict_size))
    # in Python 3: dictionary = {chr(i): chr(i) for i in range(dict_size)}

    w = result = compressed.pop(0)
    for k in compressed:
        if k in dictionary:
            entry = dictionary[k]
        elif k == dict_size:
            entry = w + w[0]
        else:
            raise ValueError('Bad compressed k: %s' % k)
        result += entry

        # Add w+entry[0] to the dictionary.
        dictionary[dict_size] = w + entry[0]
        dict_size += 1

        w = entry
    return result
如何使用:

输出

['T', 'O', 'B', 'E', 'O', 'R', 'N', 'O', 'T', 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT

注意:此示例取自

您所拥有的是否有问题?您可能无法打印它并期望它是人类可读的,但您可以将它保存在某个地方。你的代码在我看来是正确的。啊,对,谢谢-我确实尝试过删除“print enc”并将其替换为“lzw.writebytes(output.txt,enc)”,但对此也没有任何乐趣=(感谢你的帮助,是的,我一直在尝试使用来自package API站点的信息,但迄今为止失败=(关于第二个链接,我是否应该将伪代码翻译成python并使用它呢?我必须正确地阅读它,并试图理解发生了什么,它显然比我最初想象的要复杂得多-我以为一个简单的5-6行脚本就可以涵盖它!我现在就要开始了,非常感谢saher!没问题。很高兴我能我可以帮你。如果你有问题,我也可以帮你理解代码。
['T', 'O', 'B', 'E', 'O', 'R', 'N', 'O', 'T', 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT