我试图打开多个 .cdf 文件并将它们存储在字典中,但是当我尝试在 pycdf.CDF() 命令中使用通配符时,返回此错误:spacepy.pycdf.CDFError: NO_SUCH_CDF: 指定的 CDF 不存在。
.cdf 文件具有一组初始名称 (instrumentfile)、一个日期 (20010101),然后是一个可变部分(可以是 1、2、3 或 4)。这意味着我不能简单地编写如下代码:
DayCDF = pycdf.CDF('/home/location/instrumentfile'+str(dates)+'.cdf')
我还需要更改分配给 .cdf 数据的变量的名称,因此我尝试将数据导入字典(也不确定这是否可行)。
当前代码如下所示:
dictDayCDF = {}
for x in range(len(dates)):
dictDayCDF["DayCDF"+str(x)] = pycdf.CDF('/home/location/instrumentfile'+str(dates[x])+'*.cdf')
并返回错误 spacepy.pycdf.CDFError: NO_SUCH_CDF: The specified CDF does not exist.
我也尝试过使用 glob.glob,因为我在类似问题的答案中看到了这个建议,但我无法弄清楚如何将命令应用于打开 .cdf 文件:
dictDayCDF = {}
for x in range(len(dates)):
dictDayCDF["DayCDF"+str(x)] = pycdf.CDF(glob.glob('/home/location/instrumentfile'+str(dates[x])+'*.cdf'))
返回此错误:ValueError: pathname must be string-like
预期结果是一个 .cdf 文件的字典,可以使用名称 DayCDF1、DayCDF2 等调用,无论结束变量部分如何都可以导入。
请您参考如下方法:
如何从以下代码框架开始:
import glob
for file_name in glob.glob('./*.cdf'):
print(file_name)
#do something else with the file_name
至于您遇到的错误消息的根本原因:如果您 check the documentation of the method you're trying to use , 表示
Open or create a CDF file by creating an object of this class. Parameters:
pathname : string
name of the file to open or create
基于此,我们可以推断它需要单个文件名,不是文件名列表。当您尝试强制使用文件名列表时,即使用 glob 的结果,它会像您观察到的那样提示。
