这就是我刚刚使用和工作的内容。@arne的答案已针对Python 3更新,还包括复制变量属性:
import netCDF4 as nctoexclude = ['ExcludeVar1', 'ExcludeVar2']with netCDF4.Dataset("in.nc") as src, netCDF4.Dataset("out.nc", "w") as dst: # copy global attributes all at once via dictionary dst.setncatts(src.__dict__) # copy dimensions for name, dimension in src.dimensions.items(): dst.createDimension( name, (len(dimension) if not dimension.isunlimited() else None)) # copy all file data except for the excluded for name, variable in src.variables.items(): if name not in toexclude: x = dst.createVariable(name, variable.datatype, variable.dimensions) dst[name][:] = src[name][:] # copy variable attributes all at once via dictionary dst[name].setncatts(src[name].__dict__)


