Save and Read Numpy Data in gz format

Learnt it recently. A neat hidden feature in numpy.

I use loadtxt and savetxt to transfer ASCII coordinates and other information to and fro with an in house finite element software.

The quick plotting and ready availability of high level scientific libraries makes it easy to get a quick prototype up and running in python. And ASCII format files are the easiest way these models are read back into the fem software.

But the data sometimes becomes big and writing as text file do consume good mb of space.

Yesterday, accidentally found that if in savetxt, we use .gz as the file extension then python natively compresses the data using gzip compression.

That’s great. Reduces the size of the files by a factor of 80 %. And what’s more useful is, loadtxt can natively read a .gz file !! So getting zipped data back into python is just a single command.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s