nc和hdf是一家人,5行Python代碼一鍋端
網上各種讀取nc和hdf格式的教程很多,我總結了一個簡單暴力、性價比相當高的套路,總共只要4~5行Python代碼(核心代碼只有2行),通吃netCDF3、netCDF4、hdf3、hdf4:
import netCDF4 as nc
filename = r"E:whatever.nc" # 也可以是hdf文件
with nc.Dataset(filename) as file:
file.set_auto_mask(False) # 可選
variables = {x: file[x][()] for x in file.variables}
最終nc或hdf中的所有變數以字典的形式存在variables
中了。
TAG:Python |