且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理在不同Python版本中引发的不同异常

更新时间:2023-09-11 13:49:28

这不漂亮,但它应该可行...

This isn't pretty, but it should be workable ...

ParseError = xml.parsers.expat.ExpatError if sys.version < (2, 7) else xml.etree.ElementTree.ParseError

try:
    ...
except ParseError:
    ...

您可能需要根据版本修改您导入的内容(或捕获 xml 导入各种子模块,如果它们不存在于python2.6上,那么导入错误 - 我没有安装该版本我现在不能做一个强大的测试...)

You might need to modify what you import based on versions (or catch ImportError while importing the various submodules from xml if they don't exist on python2.6 -- I don't have that version installed, so I can't do a robust test at the moment...)