Note that there are some explanatory texts on larger screens.

plurals
  1. POHuffman encoding: how to write binary data in Python
    primarykey
    data
    text
    <p>I have tried methods using the struct module, as shown by the lines commented out in my code, but it didn't work out. Basically I have two options: I can either write the binary data code by code (my code are sequences of bits of length varying from 3 to 13 bits), or convert the whole string of n characters (n=25000+ in this case) to binary data. But I don't know how to implement either methods. Code:</p> <pre><code>import heapq import binascii import struct def createFrequencyTupleList(inputFile): frequencyDic = {} intputFile = open(inputFile, 'r') for line in intputFile: for char in line: if char in frequencyDic.keys(): frequencyDic[char] += 1 else: frequencyDic[char] = 1 intputFile.close() tupleList = [] for myKey in frequencyDic: tupleList.append((frequencyDic[myKey],myKey)) return tupleList def createHuffmanTree(frequencyList): heapq.heapify(frequencyList) n = len(frequencyList) for i in range(1,n): left = heapq.heappop(frequencyList) right = heapq.heappop(frequencyList) newNode = (left[0] + right[0], left, right) heapq.heappush(frequencyList, newNode) return frequencyList[0] def printHuffmanTree(myTree, someCode,prefix=''): if len(myTree) == 2: someCode.append((myTree[1] + "@" + prefix)) else: printHuffmanTree(myTree[1], someCode,prefix + '0') printHuffmanTree(myTree[2], someCode,prefix + '1') def parseCode(char, myCode): for k in myCode: if char == k[0]: return k[2:] if __name__ == '__main__': myList = createFrequencyTupleList('input') myHTree = createHuffmanTree(myList) myCode = [] printHuffmanTree(myHTree, myCode) inputFile = open('input', 'r') outputFile = open('encoded_file2', "w+b") asciiString = '' n=0 for line in inputFile: for char in line: #outputFile.write(parseCode(char, myCode)) asciiString += parseCode(char, myCode) n += len(parseCode(char, myCode)) #values = asciiString #print n #s = struct.Struct('25216s') #packed_data = s.pack(values) #print packed_data inputFile.close() #outputFile.write(packed_data) outputFile.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload