【问题标题】:How can I turn off error printing in libxml2.parseDoc?如何关闭 libxml2.parseDoc 中的错误打印?
【发布时间】:2014-12-03 05:26:50
【问题描述】:

使用该库时,我希望输入错误会出现异常,但我不希望它开始将内容打印到 stderr。如何将其配置为不打印任何内容?

这是我正在谈论的 REPL 中的一个示例:

>>> import libxml2
>>> try:
...     libxml2.parseDoc('junk')
... except:
...     pass
... 
Entity: line 1: parser error : Start tag expected, '<' not found
junk
^
>>> 

使用该代码,我希望什么都不会打印出来。我发现 this SO post 关于 c++ xmllib2 的类似问题,但我没有看到使用 python 版本的方法。

【问题讨论】:

    标签: python xml libxml2


    【解决方案1】:

    您可以为libxml2禁用错误日志记录,方法是注册一个静默错误处理程序:

    def noerr(ctx, str):
        pass
    
    libxml2.registerErrorHandler(noerr, None)
    

    来源:http://xmlsoft.org/python.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      相关资源
      最近更新 更多