python的config包(PythonConfigParser配置文件变得简单)

python的config包(PythonConfigParser配置文件变得简单)(1)

如果你想构建对更新稳健且易于管理的 Python 项目,ConfigParser 是一个不错的选择。

本篇文章将带你了解如何使用 configparser 模块为你的应用程序读取和写入配置文件。

什么是 Python ConfigParser 模块?

配置文件,俗称配置文件,常用于配置操作系统和应用程序的参数、选项和设置。

配置文件存储应用程序在启动时读取并用作其逻辑的一部分的键值对。

ConfigParser 是一个 Python 模块,它允许以简单的方式创建配置文件。使用 ConfigParser,你还可以读取或更新配置文件的内容。使用 ConfigParser 创建的配置文件的结构类似于 Microsoft Windows INI 文件的结构。

将配置与代码分离将为你省去从头开始编译和重新部署代码以进行配置更改的麻烦。

如何使用 Python ConfigParser 编写 INI 配置文件

让我们看一个简单的配置文件,如下所示。将其保存在你的机器上名为sample_config.ini的文件中。

[DEFAULT] serverthreads = 45 content_type = text/html content_length = 1024 autoscaling = no [website_name.org] username = your_name login_mail = your_mail [private.data.com] password = secret_password autoscaling = yes

在配置文件中,你将在方括号中看到一个部分的名称。节是配置文件的基础。

在每个部分下,键值对用赋值符号“=”分隔。每个部分下的每个键值都必须是唯一的。

现在让我们看看如何使用 configparser 创建这个配置文件:

import configparser config = configparser.ConfigParser() config['DEFAULT'] = { 'serverthreads' : 45, 'content_type' : 'text/html', 'content_length' : 1024 } config['DEFAULT']['autoscaling'] = 'no' config['website_name.org'] = dict() config['website_name.org']['Username'] = 'your_name' config['website_name.org']['login_mail'] = 'your_mail' config['private.data.com'] = {} private = config['private.data.com'] private['Password'] = 'secret_password' private['autoscaling'] = 'yes' with open('generated_config.ini', 'w') as configfile: config.write(configfile)

上面的代码示例创建一个名为generate_config.ini的配置文件。

Python 如何处理配置文件

从上一节的示例中可以看出,你可以像处理字典一样处理配置文件。

但是,重要的是要注意区别……

配置部分下的所有键都不区分大小写:你可以通过验证以下表达式是否都返回布尔值True来确认这一点。

你可以在前面生成配置文件的 Python 代码的末尾添加以下代码。

print('ServerThreads' in config['DEFAULT']) print('serverthreads' in config['DEFAULT']) [output] True True

如何使用 Python ConfigParser 读取 INI 配置文件

现在我们知道如何创建配置文件,是时候学习如何读取配置文件了。

我们将使用 Python shell 来帮助我们轻松地测试不同的表达式。

你可以简单地从初始化配置解析器开始。请注意,在不读取任何配置文件的情况下,配置对象会返回一个空的部分列表。

import configparser config = configparser.ConfigParser() config.sections()

现在使用 config 对象的 read 方法来读取我们创建的配置文件的内容:

config.read('generated_config.ini')

然后,你可以按如下方式获取部分名称,尽管 DEFAULT 部分不会显示在列表中:

config.sections()

运行输出:

python的config包(PythonConfigParser配置文件变得简单)(2)

从 INI 文件中读取配置文件后,让我们对配置文件执行一些判断语句

import configparser config = configparser.ConfigParser() config.read('generated_config.ini') 'unknown_property' in config 'your_name' in config['website_name.org']['username'] config['private.data.com']['password'] for key in config['DEFAULT']: print(key)

运行输出:

python的config包(PythonConfigParser配置文件变得简单)(3)

如何使用 ConfigParser 从配置文件中获取值

与字典类似,你可以使用get() 方法来检索与键对应的值。

import configparser config = configparser.ConfigParser() config.read('generated_config.ini') website = config['website_name.org'] mail = website.get('login_mail') print(mail)

运行输出:

python的config包(PythonConfigParser配置文件变得简单)(4)

此外,get() 方法还允许一个回退值,如果在该部分下找不到请求的键,则返回一个值:

website.get('login_date', fallback='No login_date found')

python的config包(PythonConfigParser配置文件变得简单)(5)

重要的是要注意 DEFAULT 部分与其他部分相比具有层次优势,并且与 DEFAULT 部分中的键对应的值将优先于备用值

website.get('content_type', fallback='No login_date found')

python的config包(PythonConfigParser配置文件变得简单)(6)

使用 Python ConfigParser 更新配置文件中的值

使用 configparser 更新配置文件中的键名

import configparser config = configparser.ConfigParser() config.read('generated_config.ini') config['private.data.com']['password'] = 'new_password' config['website_name.org']['login_date'] = 'new_date' config['new_section'] = {'user_port': 4098, 'user_request': 'yes'} with open('generated_config.ini', 'w') as configfile: config.write(configfile)

运行查看一下文件是否更新

python的config包(PythonConfigParser配置文件变得简单)(7)

结论

在本篇文章中,你学习了:

  • 什么是配置文件。
  • Python configparser 模块如何让你读取、写入和更新配置文件。

使用 configparser,你现在知道如何简化管理应用程序配置的方式。

如果你发现我的任何文章有帮助或有用,麻烦点赞或者转发。 谢谢!

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页