Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6488be7

Browse files
committed
Add gzip support to save/load
svn path=/trunk/matplotlib/; revision=941
1 parent ac68d96 commit 6488be7

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

lib/matplotlib/pylab.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
exception of those in mlab.py provided by matplotlib.
184184
"""
185185

186-
186+
import gzip
187187
import cm
188188
import _pylab_helpers
189189
import mlab #so I can override hist, psd, etc...
@@ -941,7 +941,8 @@ def load(fname,comments='%'):
941941
942942
The data must be regular, same number of values in every row
943943
944-
fname can be a filename or a file handle
944+
fname can be a filename or a file handle. Support for gzipped files is
945+
automatic, if the filename ends in .gz
945946
946947
matfile data is not currently supported, but see
947948
Nigel Wade's matfile ftp://ion.le.ac.uk/matfile/matfile.tar.gz
@@ -967,7 +968,10 @@ def load(fname,comments='%'):
967968
"""
968969

969970
if is_string_like(fname):
970-
fh = file(fname)
971+
if fname.endswith('.gz'):
972+
fh = gzip.open(fname)
973+
else:
974+
fh = file(fname)
971975
elif hasattr(fname, 'seek'):
972976
fh = fname
973977
else:
@@ -1004,7 +1008,9 @@ def save(fname, X, fmt='%.18e'):
10041008
Save the data in X to file fname using fmt string to convert the
10051009
data to strings
10061010
1007-
fname can be a filename or a file handle
1011+
fname can be a filename or a file handle. If the filename ends in .gz,
1012+
the file is automatically saved in compressed gzip format. The load()
1013+
command understands gzipped files transparently.
10081014
10091015
Example usage:
10101016
@@ -1016,7 +1022,10 @@ def save(fname, X, fmt='%.18e'):
10161022
"""
10171023

10181024
if is_string_like(fname):
1019-
fh = file(fname, 'w')
1025+
if fname.endswith('.gz'):
1026+
fh = gzip.open(fname,'wb')
1027+
else:
1028+
fh = file(fname,'w')
10201029
elif hasattr(fname, 'seek'):
10211030
fh = fname
10221031
else:

0 commit comments

Comments
 (0)