Home » Blog » File Input / Output package

File Input / Output package

File Input / Output package:

Scipy, I/O package, has a wide range of functions for work with different files format which are Matlab, Arff, Wave, Matrix Market, IDL, NetCDF, TXT, CSV and binary format.

Let’s we take one file format example as which are regularly use of MatLab:

 import numpy as np
 from scipy import io as sio
 array = np.ones((4, 4))
 sio.savemat('example.mat', {'ar': array}) 
 data = sio.loadmat(‘example.mat', struct_as_record=True)
 data['ar']

Output:

array([[ 1., 1., 1., 1.],
           [ 1., 1., 1., 1.],
           [ 1., 1., 1., 1.],
           [ 1., 1., 1., 1.]])

Code Explanation

  • Line 1 & 2: Import the essential library scipy with i/o package and Numpy.
  • Line 3: Create 4 x 4, dimensional one’s array
  • Line 4: Store array in example.mat file.
  • Line 5: Get data from example.mat file
  • Line 6: Print output.

Leave a Reply

Your email address will not be published.