Python:configparser 兼容 2.x 与 3.x

  configparser 在 python 的 2.x 版本与 3.x 版本引入的区别只是字母的大小写而已(3.x 版本加入了更多的功能),不过在 python2.x 中是直接有 Configparser 插件,而 python3.x 需要自己去安装:

pip3 install configparser

保证在 Python 环境中已经安装了 configparser 之后,使用以下代码兼容 configparser

try:
    import configparser as configparser
except Exception:
    import ConfigParser as configparser
con = configparser.ConfigParser()
con.read("xxxx")  # 文件名
print(con.sections())

猜你喜欢

转载自blog.csdn.net/qq_33811662/article/details/80656019