示例YAML文件在PyYaml模块中显示错误?

时间:2015-01-03 23:04:55

标签: python yaml

我看到Sublime Pretty Yaml代码使用pyyaml

try:
   obj = yaml.load(self.view.substr(selection))
   ....
except:
   ...
   #show error in statusbar

所以pyyaml可能会例外。如何查看,上面的代码中用什么示例文件来查看错误?

1 个答案:

答案 0 :(得分:2)

我不确定我理解你的问题。如果您要查找例外文本,请按the example in the pyyaml docs。换句话说,继续你的例子:

try:
   obj = yaml.load(self.view.substr(selection))
   ....
except yaml.YAMLError, exc:
    if hasattr(exc, 'problem_mark'):
        mark = exc.problem_mark
        errormessage = "Error position: (%s:%s)" % (mark.line+1, mark.column+1)
    else:
        errormessage = "Something else went wrong with ..."
    # do whatever you want with errormessage