如何在python3中把二进制的1和0存储为比特而不是字节?

问题描述 投票:0回答:1

我想在python中使用huffman编码进行文件压缩,我已经成功地构建了文件中每个独特字符的代码。现在,当我用这个代码对原始文件进行编码时,它产生了一个1和0的序列。然而,每个字符需要一个字节,我想知道如何存储代码,使每个1s和0s在python3中被存储为比特而不是字节,这实际上将减少文件的大小。

#This is the file i redirect my output to
sys.stdout=open("./input2.txt","w")
#Tree for storing the code
class Tree:
      __slots__=["left","right","val","c"]
      def __init__(self,val,c):
          self.val=val
          self.c=c
          self.left=self.right=None
      def value(self):
          return (self.val,self.c)

#Tree is a list of tree nodes. Initially it is a list where each 
#character is a seperate  tree.
def construct(tree):
    while(len(tree)>1):
        left=_heapq.heappop(tree)
        right=_heapq.heappop(tree)
        root=(left[0]+right[0],left[1]+right[1],Tree(left[0]+right[0],left[1]+right[1]))
        root[2].left=left[2]
        root[2].right=right[2]
        _heapq.heappush(tree,root)
    return tree

#This function generates the code for the characters in the tree which
#is the 'root' argument and 'code' is an empty String
#'codes' is the map for mapping character with its code
def Print(root,code,codes):
    if(root.left==None and root.right==None):
        codes[root.c]=code
        return 
    Print(root.left,code+'0',codes)
    Print(root.right,code+'1',codes)

#This function encodes the 'compressed' string with the 'codes' map
def encode(compressed,codes):
    document=''.join(list(map(lambda x:codes[x],compressed)))
    return document

我的输出是这样的。

110111001110111001110111001110111001110101000011011011011110101001111011001101110100111101101111011100011110110111101011111101010111010000011011101011101101111011101111011110111011001101001101110100011101111011101101010110

问题是每个1和0都存储为一个 性格4个字节 每一个,我希望它们被存储为

python-3.6 huffman-code
1个回答
0
投票

你没有包括你保存到文件的代码,所以我不能确定。然而,我可以在这里采取一个猜测。

你很可能忘了把你的1和0代码打包在一起。您可能需要使用 bytesbytearray型(见 此处 文档),并使用位运算符(见 此处 以了解更多信息),将您的8个代码转移并打包到每个 byte 前,将其存储到文件中。

要注意将代码打包到文件中的位序。bytes.

我没有使用过这些,但你可能会发现 pack 常规有用。见 此处 信息。

© www.soinside.com 2019 - 2024. All rights reserved.