self.fp = open(self.filename, '%sb' % omode) 类型错误: 强制到 Unicode: 需要字符串或缓冲区, 找到字段



我正在尝试运行python代码来后处理netcdf数据,并调用一个函数,我得到错误:self.fp = open(self.filename, '%sb' % omode( 类型错误:强制到 Unicode:需要字符串或缓冲区,找到字段

我放置了函数和调用它的代码部分。有人可以告诉我如何解决问题??我想这是一个非常愚蠢的问题,但我找不到解决它的方法,因为我必须打开的文件是一个 netcdf 文件,它使用类来读取文件本身。

class DataFieldForOHstarSimulation(object):
def __init__(self, solution_file, grid_file, use_precomputed_values=True):
# open the solution file
self.solution_netcdf_file = sp.io.netcdf.netcdf_file(solution_file)
# create shortcut to vars
self.vars = self.solution_netcdf_file.vars
# open the grid file
self.grid_netcdf_file = sp.io.netcdf.netcdf_file(grid_file)
# make shortcut to points; assign empty array
self.pnt = np.empty((3, 
self.grid_netcdf_file.dimensions["no_of_points"]))
# asign coordinates to points, convert to metres
self.pnt[:] = self.grid_netcdf_file.variables['points_xc'][:]*0.001, 
self.grid_netcdf_file.variables['points_yc'][:]*0.001, 
self.grid_netcdf_file.variables['points_zc'][:]*0.001
# save the physical limits of the data
self.extents = np.vstack([np.min(self.pnt, axis=1),
np.max(self.pnt, axis=1)])
# build the KDTree to access points close to the arbitrary point
self.tree = sp.spatial.cKDTree(self.pnt.T, leafsize=120)
# do we want to compute spectra for every point or use the
# approximations from the pre-computed intrpolators
self.use_precomputed_values = use_precomputed_values
# calculate the refractive index for each point
self.refractiveIndex()

# create solution field object
solutionField = Field(solution_file)
# create grid object
grid = Grid(grid_file)
# create main data field object
dataField = DataFieldForOHstarSimulation(solutionField,grid, use_precomputed_values=use_precomputed_values)

然后我得到错误:

self.fp = open(self.filename, '%sb' % omode)

类型错误:强制到 Unicode:需要字符串或缓冲区,找到字段

我解决了这个问题,将文件作为 netcdf 而不是字符串调用时出错。无论如何,感谢您对我的第一个问题的支持!!

最新更新