从不同目录读取时如何避免不支持的 .nc 文件
How to avoid unsupported .nc file while reading from different directory
我在包含 .nc
个文件的目录中有几个文件夹。阅读时出现错误:'NETCDF can not read unsupported file'。由于有超过 5000 个文件,我不知道哪个文件已损坏或不受支持。是否有通过跳转到另一个支持的文件来读取文件的方法。
我使用的代码是:
import xarray as xr
import numpy as np
import pandas as pd
ncfile = glob.glob('mydata/****/se*')
frame = pd.DataFrame()
for i in np.arange(len(ncfile)):
frame = frame
for j in np.arange(len(ds.variables['time'])):
ds1 = xr.open_dataset(ncfile[i])
prec = np.ravel(ds.variables['precipitation_amount'][j,:,:])
frame[dates] = prec
ds = xr.open_dataset(ncfile[i])
您可以使用异常处理来做到这一点。我已经根据你的代码用一个简单的例子展示了这一点:
import xarray as xr
import numpy as np
import pandas as pd
ncfile = glob.glob('mydata/****/se*')
frame = pd.DataFrame()
errors = []
for i in np.arange(len(ncfile)):
frame = frame
try:
ds = xr.open_dataset(ncfile[i])
except:
errors.append(ncfile[i])
我在包含 .nc
个文件的目录中有几个文件夹。阅读时出现错误:'NETCDF can not read unsupported file'。由于有超过 5000 个文件,我不知道哪个文件已损坏或不受支持。是否有通过跳转到另一个支持的文件来读取文件的方法。
我使用的代码是:
import xarray as xr
import numpy as np
import pandas as pd
ncfile = glob.glob('mydata/****/se*')
frame = pd.DataFrame()
for i in np.arange(len(ncfile)):
frame = frame
for j in np.arange(len(ds.variables['time'])):
ds1 = xr.open_dataset(ncfile[i])
prec = np.ravel(ds.variables['precipitation_amount'][j,:,:])
frame[dates] = prec
ds = xr.open_dataset(ncfile[i])
您可以使用异常处理来做到这一点。我已经根据你的代码用一个简单的例子展示了这一点:
import xarray as xr
import numpy as np
import pandas as pd
ncfile = glob.glob('mydata/****/se*')
frame = pd.DataFrame()
errors = []
for i in np.arange(len(ncfile)):
frame = frame
try:
ds = xr.open_dataset(ncfile[i])
except:
errors.append(ncfile[i])