Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:44:52+00:00 2026-05-23T22:44:52+00:00

I have tried methods using the struct module, as shown by the lines commented

  • 0

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:

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:44:52+00:00Added an answer on May 23, 2026 at 10:44 pm

    You’re looking for this:

    packed_data = ''.join(chr(int(asciiString[i:i+8], 2)) 
                             for i in range(0, len(asciiString), 8))
    

    It will take 8 bits at a time from the asciiString, interpret it as an integer, and output the corresponding byte.

    Your problem here is that this requires the length of asciiString to be a multiple of 8 bits to work correctly. If not, you’ll insert zero bits before the last few real bits.

    So you need to store the number of bits in the last byte somewhere, so you know to ignore those bits when you get them back, instead of interpreting them as zeros. You could try:

    packed_data = chr(len(asciiString) % 8) + packed_data
    

    Then when you read it back:

    packed_input = coded_file.read()
    last_byte_length, packed_input, last_byte = (packed_input[0], 
                                                 packed_input[1:-1], 
                                                 packed_input[-1])
    if not last_byte_length: last_byte_length = 8
    ascii_input = ''.join(chain((bin(ord(byte))[2:].zfill(8) for byte in packed_input),
                          tuple(bin(ord(last_byte))[2:].zfill(last_byte_length),)))
    # OR
    # ascii_input = ''.join(chain(('{0:0=8b}'.format(byte) for byte in packed_input),
    #                       tuple(('{0:0=' + str(last_byte_length) + '8b}').format(last_byte),)))
    

    Edit: You either need to strip ‘0b’ from the strings returned by bin() or, on 2.6 or newer, preferably use the new, alternate versions I added that use string formatting instead of bin(), slicing, and zfill().

    Edit: Thanks eryksun, good to use chain to avoid making a copy of the ASCII string. Also, need to call ord(byte) in the bin() version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried using the obvious method as outlined in the following example but
I've tried two different methods of reusing code. I have a solution full of
I have read an example and tried to duplicate it's methods but with weird
I have tried to find how to create DLL-s on linux using google, but
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried to integrate the Picasa API on iPhone, compiles fine, but I
I have written the following C99 code and was wondering about the struct declaration.
I have a method with an out parameter that tries to do a type
I have tried <ul id=contact_list> <li id=phone>Local 604-555-5555</li> <li id=i18l_phone>Toll-Free 1-800-555-5555</li> </ul> with #contact_list
I have tried what seems like everything - I've done similiar things many times

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.