简体   繁体   English

Python2 移植到 Python3:ConfigParser 异常 - MissingSectionHeaderError 上的 AttributeError

[英]Python2 porting to Python3: ConfigParser Exceptions - AttributeError on MissingSectionHeaderError

I'm attempting to port a Python2.7 script to Python3.6+ and have hit a roadblock my google searching couldn't resolve.我正在尝试将 Python2.7 脚本移植到 Python3.6+ 并且遇到了我的谷歌搜索无法解决的障碍。 The issue is that a try: except: call below doesn't appear to work after doing some initial porting suggestions.问题是,在做了一些初始移植建议之后,下面的 try: except: call 似乎不起作用。 I'm sure this is something simple;我敢肯定这很简单; just escapes me at the moment.只是此刻逃脱了我。

Python2.7 code: (worked) Python2.7代码:(工作)

import ConfigParser
logOutCfg = ConfigParser.ConfigParser()

try:
 if (os.path.isfile(logOutfilename)) : logOutCfg.read(logOutfilename)
except ConfigParser.MissingSectionHeaderError as e:
 pass
except ConfigParser.ParsingError as e:
 print(str(e))
 pass

Python3.6 attempted code (doesn't work under Python 2.7): Python3.6尝试的代码(在 Python 2.7 下不起作用):

from configparser import ConfigParser
logOutCfg = ConfigParser()

try:
 if (os.path.isfile(logOutfilename)) : logOutCfg.read(logOutfilename)
except ConfigParser.MissingSectionHeaderError as e:
 pass
except ConfigParser.ParsingError as e:
 print(str(e))
 pass

The error reported by the script when running under Python2 is:脚本在Python2下运行报错为:

 File "<script>.py", line 242, in <function>
    except ConfigParser.MissingSectionHeaderError:
AttributeError: type object 'ConfigParser' has no attribute 'MissingSectionHeaderError'

I'm pretty sure I tried a bunch of different things;我很确定我尝试了很多不同的东西。 including: except configparser.MissingSectionHeaderError but with no joy.包括: except configparser.MissingSectionHeaderError但没有喜悦。

What am I missing?我错过了什么? I need the code to work in both Python2 and Python3 for the foreseeable future... at least the next 9 months.在可预见的未来……至少在接下来的 9 个月内,我需要代码在 Python2 和 Python3 中工作。

@mkrieger1 was right on the money. @mkrieger1 是对的。 The key was to also import configparser关键是还要导入 configparser

import configparser
from configparser import ConfigParser

logOutCfg = ConfigParser()

try:
 if (os.path.isfile(logOutfilename)) : logOutCfg.read(logOutfilename)
except configparser.MissingSectionHeaderError as e:
 pass
except configparser.ParsingError as e:
 print(str(e))
 pass

Your Python 3.6 code is mostly correct.您的 Python 3.6 代码大部分是正确的。 The only problem is that you are trying to grab the exceptions from the ConfigParser class instead of the configparser module.唯一的问题是您试图从ConfigParser class 而不是configparser模块中获取异常。 The main confusion here is most likely coming from the fact that name of the module changed in Python 3 to be more inline with PEP8 standards .这里的主要混淆很可能来自于模块名称在 Python 3 中更改为更符合PEP8 标准的事实。 Originally, the ConfigParser module and class shared the same name, but this is not the case in Python 3.最初, ConfigParser模块和 class 共享相同的名称,但在 Python 3 中并非如此。

The below code should work in Python 2 and 3.下面的代码应该在 Python 2 和 3 中工作。

try:
    import configparser
except ImportError: 
    import ConfigParser as configparser

logOutCfg = configparser.ConfigParser()

try:
    if (os.path.isfile(logOutfilename)):
        logOutCfg.read(logOutfilename)
except configparser.MissingSectionHeaderError as e:
    pass
except configparser.ParsingError as e:
    print(e)

Here is a quick breakdown of what is going on here.这是这里发生的事情的快速细分。

try:
    import configparser
except ImportError: 
    import ConfigParser as configparser

Try to import the Python 3 configparser module.尝试导入 Python 3 configparser模块。 If in Python 2, this will raise an ImportError .如果在 Python 2 中,这将引发ImportError We catch that, and instead import ConfigParser and alias it with configparser .我们抓住了这一点,而是导入ConfigParser并使用configparser为其别名。

logOutCfg = configparser.ConfigParser()

Grab the ConfigParser class from the configparser module imported above, and instantiate it.从上面导入的configparser模块中ConfigParser class,并将其实例化。

try:
    if (os.path.isfile(logOutfilename)):
        logOutCfg.read(logOutfilename)
except configparser.MissingSectionHeaderError as e:
    pass
except configparser.ParsingError as e:
    print(e)

Try to open the file as you were before, but instead of attempting to get the exceptions from the ConfigParser class, get them from the configparser module.尝试像以前一样打开文件,但不要尝试从ConfigParser class 获取异常,而是从configparser模块获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM