Python: How to convert Huffman code int to actual bytes to store into file
I am attempting to compress a text file with the huffman compression algorithm. My text was "go go gophers" (for testing purposes of course). I finished creating a huffman tree & code and replaced my text with my huffman code. I got my encoded text as shown below.
I realized that my huffman code in string or integer is greater than my actual raw text. I thought what if i converted my huffman string (or integer) into bytes then create a file that writes the file using my huffman integers.
I'm attempting to compress my file with binary code so that the length of my code is actually 6 bytes (The length of my binary integer / 8). Is there any way to convert my binary integers to actual bytes then code it into a new file? What i mean is that each individual character of my binary integers will be considered as one bit in my file. Is this possible? Is there a better alternative that doesn't use bytearray()?
Code:
print("Index: ", Index) # The Index
# Subsituting text to our compressed index
for x in range(len(TextTest)):
TextTest[x]=Index[TextTest[x]]
NewText=''.join(TextTest)
Excess = int(8-(len(NewText)%8))
NewText= NewText+ '0'*Excess
print("Encoded Text in bits:",NewText)
print('Assuming each huffman code is a bit, the Compressed text will have the length of :',len(NewText)/8 ,"bytes")
print("The size of the integer code is :",sys.getsizeof(int(NewText)),'bytes')
print("Size of index is:",sys.getsizeof(str(Index)))
print("This is a total compression text size of:",sys.getsizeof(str(Index))+len(NewText)/8)
Compressed=open("Compressed.bin",'w+')
Output:
('Raw file Size:', 279, 'bytes')
{'g': 3, 'o': 3, ' ': 3, 'p': 1, 'h': 1, 'e': 1, 'r': 1, 's': 1}
Index: {'o': '00', ' ': '01', 's':'100', 'p': '1010', 'h': '1011',
'e': '1100', 'r': '1101', 'g': '111'}
Encoded Text in bits: 111000111100011110010101011110011011000100000000
Assuming each huffman code is a bit, the Compressed text will have the length of : 6.0 bytes
The size of the integer code is : 40 bytes
Size of index is: 147
This is a total compression text size of: 153.0
Text Compressed!
python-3.x file byte binary-data huffman-code
add a comment |
I am attempting to compress a text file with the huffman compression algorithm. My text was "go go gophers" (for testing purposes of course). I finished creating a huffman tree & code and replaced my text with my huffman code. I got my encoded text as shown below.
I realized that my huffman code in string or integer is greater than my actual raw text. I thought what if i converted my huffman string (or integer) into bytes then create a file that writes the file using my huffman integers.
I'm attempting to compress my file with binary code so that the length of my code is actually 6 bytes (The length of my binary integer / 8). Is there any way to convert my binary integers to actual bytes then code it into a new file? What i mean is that each individual character of my binary integers will be considered as one bit in my file. Is this possible? Is there a better alternative that doesn't use bytearray()?
Code:
print("Index: ", Index) # The Index
# Subsituting text to our compressed index
for x in range(len(TextTest)):
TextTest[x]=Index[TextTest[x]]
NewText=''.join(TextTest)
Excess = int(8-(len(NewText)%8))
NewText= NewText+ '0'*Excess
print("Encoded Text in bits:",NewText)
print('Assuming each huffman code is a bit, the Compressed text will have the length of :',len(NewText)/8 ,"bytes")
print("The size of the integer code is :",sys.getsizeof(int(NewText)),'bytes')
print("Size of index is:",sys.getsizeof(str(Index)))
print("This is a total compression text size of:",sys.getsizeof(str(Index))+len(NewText)/8)
Compressed=open("Compressed.bin",'w+')
Output:
('Raw file Size:', 279, 'bytes')
{'g': 3, 'o': 3, ' ': 3, 'p': 1, 'h': 1, 'e': 1, 'r': 1, 's': 1}
Index: {'o': '00', ' ': '01', 's':'100', 'p': '1010', 'h': '1011',
'e': '1100', 'r': '1101', 'g': '111'}
Encoded Text in bits: 111000111100011110010101011110011011000100000000
Assuming each huffman code is a bit, the Compressed text will have the length of : 6.0 bytes
The size of the integer code is : 40 bytes
Size of index is: 147
This is a total compression text size of: 153.0
Text Compressed!
python-3.x file byte binary-data huffman-code
add a comment |
I am attempting to compress a text file with the huffman compression algorithm. My text was "go go gophers" (for testing purposes of course). I finished creating a huffman tree & code and replaced my text with my huffman code. I got my encoded text as shown below.
I realized that my huffman code in string or integer is greater than my actual raw text. I thought what if i converted my huffman string (or integer) into bytes then create a file that writes the file using my huffman integers.
I'm attempting to compress my file with binary code so that the length of my code is actually 6 bytes (The length of my binary integer / 8). Is there any way to convert my binary integers to actual bytes then code it into a new file? What i mean is that each individual character of my binary integers will be considered as one bit in my file. Is this possible? Is there a better alternative that doesn't use bytearray()?
Code:
print("Index: ", Index) # The Index
# Subsituting text to our compressed index
for x in range(len(TextTest)):
TextTest[x]=Index[TextTest[x]]
NewText=''.join(TextTest)
Excess = int(8-(len(NewText)%8))
NewText= NewText+ '0'*Excess
print("Encoded Text in bits:",NewText)
print('Assuming each huffman code is a bit, the Compressed text will have the length of :',len(NewText)/8 ,"bytes")
print("The size of the integer code is :",sys.getsizeof(int(NewText)),'bytes')
print("Size of index is:",sys.getsizeof(str(Index)))
print("This is a total compression text size of:",sys.getsizeof(str(Index))+len(NewText)/8)
Compressed=open("Compressed.bin",'w+')
Output:
('Raw file Size:', 279, 'bytes')
{'g': 3, 'o': 3, ' ': 3, 'p': 1, 'h': 1, 'e': 1, 'r': 1, 's': 1}
Index: {'o': '00', ' ': '01', 's':'100', 'p': '1010', 'h': '1011',
'e': '1100', 'r': '1101', 'g': '111'}
Encoded Text in bits: 111000111100011110010101011110011011000100000000
Assuming each huffman code is a bit, the Compressed text will have the length of : 6.0 bytes
The size of the integer code is : 40 bytes
Size of index is: 147
This is a total compression text size of: 153.0
Text Compressed!
python-3.x file byte binary-data huffman-code
I am attempting to compress a text file with the huffman compression algorithm. My text was "go go gophers" (for testing purposes of course). I finished creating a huffman tree & code and replaced my text with my huffman code. I got my encoded text as shown below.
I realized that my huffman code in string or integer is greater than my actual raw text. I thought what if i converted my huffman string (or integer) into bytes then create a file that writes the file using my huffman integers.
I'm attempting to compress my file with binary code so that the length of my code is actually 6 bytes (The length of my binary integer / 8). Is there any way to convert my binary integers to actual bytes then code it into a new file? What i mean is that each individual character of my binary integers will be considered as one bit in my file. Is this possible? Is there a better alternative that doesn't use bytearray()?
Code:
print("Index: ", Index) # The Index
# Subsituting text to our compressed index
for x in range(len(TextTest)):
TextTest[x]=Index[TextTest[x]]
NewText=''.join(TextTest)
Excess = int(8-(len(NewText)%8))
NewText= NewText+ '0'*Excess
print("Encoded Text in bits:",NewText)
print('Assuming each huffman code is a bit, the Compressed text will have the length of :',len(NewText)/8 ,"bytes")
print("The size of the integer code is :",sys.getsizeof(int(NewText)),'bytes')
print("Size of index is:",sys.getsizeof(str(Index)))
print("This is a total compression text size of:",sys.getsizeof(str(Index))+len(NewText)/8)
Compressed=open("Compressed.bin",'w+')
Output:
('Raw file Size:', 279, 'bytes')
{'g': 3, 'o': 3, ' ': 3, 'p': 1, 'h': 1, 'e': 1, 'r': 1, 's': 1}
Index: {'o': '00', ' ': '01', 's':'100', 'p': '1010', 'h': '1011',
'e': '1100', 'r': '1101', 'g': '111'}
Encoded Text in bits: 111000111100011110010101011110011011000100000000
Assuming each huffman code is a bit, the Compressed text will have the length of : 6.0 bytes
The size of the integer code is : 40 bytes
Size of index is: 147
This is a total compression text size of: 153.0
Text Compressed!
python-3.x file byte binary-data huffman-code
python-3.x file byte binary-data huffman-code
edited Nov 24 '18 at 18:38
Mohamed Alremeithi
asked Nov 24 '18 at 13:57
Mohamed AlremeithiMohamed Alremeithi
13
13
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458871%2fpython-how-to-convert-huffman-code-int-to-actual-bytes-to-store-into-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458871%2fpython-how-to-convert-huffman-code-int-to-actual-bytes-to-store-into-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown