compression

Implementing the LZ78 compression algorithm in python

Implementing the LZ78 compression algorithm in python Question: I’ve been toying around with some compression algorithms lately but, for the last couple days, I’ve been having some real trouble implementing LZ78 in python. I’ve looked around online for some examples but haven’t really found anything reliable that both encodes and decodes input. Does anyone have …

Total answers: 2

How to compress or compact a string in Python

How to compress or compact a string in Python Question: I’m making a python “script” that sends a string to a webservice (in C#). I NEED to compress or compact this string, because the bandwidth and MBs data is LIMITED (yeah, in capitals because it’s very limited). I was thinking of converting it into a …

Total answers: 2

Python – mechanism to identify compressed file type and uncompress

Python – mechanism to identify compressed file type and uncompress Question: A compressed file can be classified into below logical groups a. The operating system which you are working on (*ix, Win) etc. b. Different types of compression algorithm (i.e .zip,.Z,.bz2,.rar,.gzip). Atleast from a standard list of mostly used compressed files. c. Then we have …

Total answers: 7

Python ungzipping stream of bytes?

Python ungzipping stream of bytes? Question: Here is the situation: I get gzipped xml documents from Amazon S3 import boto from boto.s3.connection import S3Connection from boto.s3.key import Key conn = S3Connection(‘access Id’, ‘secret access key’) b = conn.get_bucket(‘mydev.myorg’) k = Key(b) k.key(‘documents/document.xml.gz’) I read them in file as import gzip f = open(‘/tmp/p’, ‘w’) k.get_file(f) …

Total answers: 4

python: read lines from compressed text files

python: read lines from compressed text files Question: Is it easy to read a line from a gz-compressed text file using python without extracting the file completely? I have a text.gz file which is aroud 200mb. When I extract it, it becomes 7.4gb. And this is not the only file I have to read. For …

Total answers: 4

Python: Reducing memory usage of dictionary

Python: Reducing memory usage of dictionary Question: I’m trying to load a couple of files into the memory. The files have either of the following 3 formats: string TAB int string TAB float int TAB float. Indeed, they are ngram statics files, in case this helps with the solution. For instance: i_love TAB 10 love_you …

Total answers: 6

Extracting zip file contents to specific directory in Python 2.7

Extracting zip file contents to specific directory in Python 2.7 Question: This is the code I am currently using to extract a zip file that lives in the same current working directory as the script. How can I specify a different directory to extract to? The code I tried is not extracting it where I …

Total answers: 6

python zipfile module doesn't seem to be compressing my files

python zipfile module doesn't seem to be compressing my files Question: I made a little helper function: import zipfile def main(archive_list=[],zfilename=’default.zip’): print zfilename zout = zipfile.ZipFile(zfilename, “w”) for fname in archive_list: print “writing: “, fname zout.write(fname) zout.close() if __name__ == ‘__main__’: main() The problem is that all my files are NOT being COMPRESSED! The files …

Total answers: 3

How to tell if a file is gzip compressed?

How to tell if a file is gzip compressed? Question: I have a Python program which is going to take text files as input. However, some of these files may be gzip compressed. Is there a cross-platform, usable from Python way to determine if a file is gzip compressed or not? Is the following reliable …

Total answers: 6

How to create full compressed tar file using Python?

How to create full compressed tar file using Python? Question: How can I create a .tar.gz file with compression in Python? Asked By: shahjapan || Source Answers: import tarfile tar = tarfile.open(“sample.tar.gz”, “w:gz”) for name in [“file1”, “file2”, “file3”]: tar.add(name) tar.close() If you want to create a tar.bz2 compressed file, just replace file extension name …

Total answers: 10