【问题标题】:Example YAML file which shows error in PyYaml module?在 PyYaml 模块中显示错误的示例 YAML 文件?
【发布时间】:2015-01-03 23:04:55
【问题描述】:

我看到Sublime Pretty Yaml 代码使用pyyaml

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

所以 pyyaml 可能会给出异常。如何查看,使用什么示例文件查看上述代码中的错误?

【问题讨论】:

  • 你想在终端或 Sublime Text 状态栏上查看 Python 程序执行的异常吗?
  • 想查看错误的 yaml 文件示例——它会在 pyyaml 中出现错误(以及 Sublime)

标签: python yaml


【解决方案1】:

我不确定我是否理解您的问题。如果您正在寻找异常的文本,请关注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

【讨论】:

  • 我的意思是别的;我需要示例 YAML 文件,它会在某些编辑器中显示错误,例如 Sublime Text;或在 Python 应用程序中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2019-04-08
  • 2018-08-01
  • 1970-01-01
  • 2016-06-01
  • 2021-05-17
  • 1970-01-01
相关资源
最近更新 更多