Search code examples
python-3.6huffman-code

How do i store binary 1s and 0s as bits rather than bytes in a file in python3?


I'm trying to do file compression using huffman encoding in python and i have successfully constructed the codes for each of the unique characters in the file. Now, when i encode the original file with this code it generates a sequence of 1s and 0s. However each character takes a byte and i want to know how i can store the codes such that each 1s and 0s gets stored as bits rather bytes in python3 which will actually reduce the file size.

#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

My output is like this:

110111001110111001110111001110111001110101000011011011011110101001111011001101110100111101101111011100011110110111101011111101010111010000011011101011101101111011101111011110111011001101001101110100011101111011101101010110

The problem is each of the 1 and 0 are stored as a character of 4 bytes each and i want them to be stored as bits


Solution

  • You do not include the code where you save it to a file so I cannot say for sure. However I can take a guess here.

    You are likely forgetting to pack your 1 and and 0 codes together. You will likely need to use the bytes or bytearraytype (see here for documentation) and use bitwise operators (see here for info) to shift and pack 8 of your codes into each byte before storing it out to the file.

    Be conscious of the bit ordering that you pack the codes into the bytes.

    I have not used these, but you might find the pack routine useful. See here for info.