Convert a .sav file to .csv file in Python

前端 未结 4 566
面向向阳花
面向向阳花 2021-01-17 07:28

I want to convert the contents of *.sav file into a *.csv file in Python. I have written the following lines of code to access the details of variables in *.sav file. Now, I

4条回答
  •  情话喂你
    2021-01-17 07:37

    I could solve my problem by changing the requisite output format and here is my code:

    import scipy.io as spio
    import numpy as np
    import csv
    
    on2file = 'ON2_2016_112m_220415.sav'   # i/p file
    outfile = 'ON2_2016_112m_220415.csv'   # o/p file
    
    # Read i/p file
    s = spio.readsav(on2file, python_dict=True, verbose=True)
    
    # Creating Grid
    #d_lat = s["d_lat"]
    #d_lon = s["d_lon"]
    lat = np.arange(-90,90,1.78218)  # (101,)
    lon = np.arange(-180,180,1.78218)     # (202,)
    ylat,xlon = np.meshgrid(lat,lon)
    
    on2grid = np.asarray(s["on2_grid"])
    on2gridsmooth = np.asarray(s["on2_grid_smooth"])
    
    nrows = len(on2grid)
    ncols = len(on2grid[0])
    
    xlon_grid = xlon.reshape(nrows*ncols,1)
    ylat_grid = ylat.reshape(nrows*ncols,1)
    on2grid_new = on2grid.reshape(nrows*ncols,1)
    on2gridsmooth_new = on2gridsmooth.reshape(nrows*ncols,1)
    
    # Concatenation
    allgriddata = np.concatenate((xlon_grid, ylat_grid, on2grid_new, on2gridsmooth_new),axis=1)
    
    # Writing o/p file
    f_handle = file(outfile,'a')
    np.savetxt(f_handle,allgriddata,delimiter=",",fmt='%0.3f',header="longitude, latitude, on2_grid, on2_grid_smooth")
    f_handle.close()
    

提交回复
热议问题