python读取sav文件_在Python中将.sav文件转换为.csv文件

该博客讲述了如何在Python中使用scipy.io库读取.sav文件,并将其内容转换成.csv文件。作者展示了如何将特定变量(如year, doy, d_lon, d_lat, on2_grid, on2_grid_smooth)写入CSV文件,同时包含标题。" 107123140,7683003,PyCharm安装与验证OpenCV教程,"['PyCharm', 'Python开发', 'OpenCV', '库管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1586010002-jmsa.png

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 am not clear on how I can write the accessed variable data to a .csv file with headers

import scipy.io as spio

on2file = 'ON2_2015_112m_220415.sav'

on2data = spio.readsav(on2file, python_dict=True, verbose=True)

Following is the result when I run the above lines of the code:

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 []

- on2_grid_smooth []

- d_lat []

- on2_grid []

- doy []

- year []

- d_lon []

--------------------------------------------------

Can anyone suggest me with how I can write all the variable data to a .csv file?

I want to write the variables (year, doy, d_lon, d_lat, on2_grid, on2_grid_smooth) to a CSV or ASCII file is supposed to look in the following manner:

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

.....

The shape of "on2_grid" and "on2_grid_smooth" variables is the same and is (101, 202). Both are of the type "numpy.ndarray".

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值