将.sav文件转换为Python中的.csv文件

问题描述:

我想将* .sav文件的内容转换为Python中的* .csv文件。我写了下面几行代码来访问* .sav文件中变量的详细信息。现在,我不是我怎么能访问的可变数据写入到与标题将.sav文件转换为Python中的.csv文件

import scipy.io as spio 
on2file = 'ON2_2015_112m_220415.sav' 
on2data = spio.readsav(on2file, python_dict=True, verbose=True) 

以下是结果,当我运行的代码上面的行.csv文件明确:

IDL Save file is compressed 
-> expanding to /var/folders/z4/r3844ql123jgkq1ztdr4jxrm0000gn/T/tmpVE_Iz6.sav 
-------------------------------------------------- 
Date: Mon Feb 15 20:41:02 2016 
User: zhangy1 
Host: augur 
-------------------------------------------------- 
Format: 9 
Architecture: x86_64 
Operating System: linux 
IDL Version: 7.0 
-------------------------------------------------- 
Successfully read 11 records of which: 
- 7 are of type VARIABLE 
- 1 are of type TIMESTAMP 
- 1 are of type NOTICE 
- 1 are of type VERSION 
-------------------------------------------------- 
Available variables: 
- saved_data [<class 'numpy.recarray'>] 
- on2_grid_smooth [<type 'numpy.ndarray'>] 
- d_lat [<type 'numpy.float32'>] 
- on2_grid [<type 'numpy.ndarray'>] 
- doy [<type 'str'>] 
- year [<type 'str'>] 
- d_lon [<type 'numpy.float32'>] 
-------------------------------------------------- 

任何人都可以告诉我如何将所有可变数据写入.csv文件?

我想写的变量(年,DOY,d_lon,d_lat,on2_grid,on2_grid_smooth)到CSV或ASCII文件应该以下列方式看:

longitude, latitude, on2_grid, on2_grid_smooth # header 
0.0,0.0,0.0,0.0    
0.0,0.0,0.0,0.0 
0.0,0.0,0.0,0.0 
0.0,0.0,0.0,0.0 
..... 

的“形状on2_grid “和”on2_grid_smooth“变量是相同的并且是(101,202)。两者都是“numpy.ndarray”类型。

+0

你想写哪个变量?什么是csv应该是什么样子?我们还应该知道要编写的数组的形状和dtype。 – hpaulj

我可以通过改变所需的输出格式,解决我的问题,在这里是我的代码:

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() 

在使用你的代码中提取的文件纬度和经度的列看起来互换。此外,纬度范围从0到180(不是+90 0 -90))... 0是否从顶部开始。 PL。评论。

+0

谢谢麦克!我已纠正。 – Madhavan

我的工作就可以了,并且就目前而言,这是我的“差”的解决方案:

首先我导入模块savReaderWriter到的.sav文件转换成结构化的阵列 其次,我导入模块numpy的转换结构数组到csv:

import savReaderWriter 
import numpy as np 

reader_np = savReaderWriter.SavReaderNp("infile.sav") 
array = reader_np.to_structured_array("outfile.dat") 
np.savetxt("outfile2.csv", array, delimiter=",") 
reader_np.close() 

问题是我在转换过程中失去了名称属性。我会尽力解决这个问题。

我知道这个解决方案使用R代替python,但它非常简单并且效果很好。

library(foreign) 
write.table(read.spss("inFile.sav"), file="outFile.csv", quote = TRUE, sep = ",")