我从实习生那里复制的Python代码中有无效语法错误

2024-06-29 00:37:44 发布

您现在位置:Python中文网/ 问答频道 /正文

三年前,我参加的大学早期课程之一是一些Python的基本培训。现在我正在寻找一个可以帮助我调整网格大小的程序,我在Python中找到了一些可以帮助我的东西。我把Python重新安装到我的电脑上,找到了我的旧编辑器。但是,当我运行代码时,我得到了一个我无法理解的无效语法错误。这是出现错误的代码部分:

def downsize(mode, cell_size, inpath, outpath):

  from VolumeData import fileformats
  try:
    grid_data = fileformats.open_file(inpath)
  except fileformats.Uknown_File_Type, e:
    sys.stderr.write(str(e))
    sys.exit(1)

  reduced = Reduced_Grid(grid_data, mode, cell_size)

  from VolumeData.netcdf.netcdf_grid import write_grid_as_netcdf
  write_grid_as_netcdf(reduced, outpath)

确切的无效语法错误在“except”中fileformats.Uknown_文件类型,e:“行。你能帮助我吗?在


Tags: 代码fromimportdatasizemodecellnetcdf
2条回答

如果您使用的是python3.x,则不能使用except fileformats.Uknown_File_Type, e。逗号用作as语句(在try/except块中),因此应该将其替换为:except fileformats.Uknown_File_Type as e。在

逗号在Python2.7中可以使用,但不能在3.x中使用。但是,as应该同时适用于这两个版本。在

参考号:Handling errors in Python 3.3

也许你拼错了“未知”?在

相关问题 更多 >