如何使用Python configparser模块解析配置文件步骤详解?

2026-05-29 03:031阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计513个文字,预计阅读时间需要3分钟。

如何使用Python configparser模块解析配置文件步骤详解?

pythonconfigparser模块在Python3中被重命名为configparser。这个模块定义了一个ConfigParser类,其作用是使配置文件生效。配置文件的格式与Windows的ini文件相同,可以直接编辑配置文件:*.ini。模板内容自定义。

ConfigParser模块在Python3修改为configparser,这个模块定义了一个ConfigeParser类,该类的作用是让配置文件生效。配置文件的格式和window的ini文件相同

编辑配置文件: .ini

如何使用Python configparser模块解析配置文件步骤详解?

模板:内容自定义

一、 编辑配置文件

import configparser config = configparser.ConfigParser() config['DEFAULT'] = { 'ServerAliveInterval':'45', 'Compression':'yes', 'CompressionLevel':'9', 'ForwardX11':'yes' } config['bitbucker.org'] = { 'Host Port':'50022', 'ForwardX11':'no' } config['path'] = { 'Base_Path':'D:\python\pychrom\路飞学城\day8', 'student_path':'D:\python\pychrom\路飞学城\day8\configparser模块.py' } with open('example.ini','w',encoding='utf-8') as configfile: config.write(configfile)

二、读取配置文件

import configparser config = configparser.ConfigParser() config.read('example.ini',encoding='utf-8') print(config.sections()) # 查看分组情况,默认default是不显示的 print('bitbucker.org' in config) # Flase 判断一个组在不在这个文件当中 print('bitbucker.com' in config) # True print(config['bitbucker.org']['host_port']) # 查钊这个文件中这个分组下面有没有这个配置 print(config['bitbucker.org']['user']) # 没有就报错 for key in config['bitbucker.org']: # 取默认分组和这个组的下面所有配置 print(key) # 只能取到 key print(config.options('bitbucker.org')) # 取分组下面的配置,包括默认分组 只能取到值 print(config.items('bitbucker.org')) # 取到分组下面的键值对,包括默认分组 print(config.get('path','base_path')) # 获取某个分组下面的键来获取值

三、增删改查

import configparser config = configparser.ConfigParser() config.read('example.ini',encoding='utf-8') config.add_section('zuming') # 添加组 config.remove_section('zuming') # 删除一个组 config.remove_option('bitbucker.org','host_port') # 删除某个组中的某一项 config.set('bitbucker.org','host_port','22') # 修改某个组下面的值 config.write(open('example.ini','w',encoding='utf-8')) # 必须添加这句话才能生效

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计513个文字,预计阅读时间需要3分钟。

如何使用Python configparser模块解析配置文件步骤详解?

pythonconfigparser模块在Python3中被重命名为configparser。这个模块定义了一个ConfigParser类,其作用是使配置文件生效。配置文件的格式与Windows的ini文件相同,可以直接编辑配置文件:*.ini。模板内容自定义。

ConfigParser模块在Python3修改为configparser,这个模块定义了一个ConfigeParser类,该类的作用是让配置文件生效。配置文件的格式和window的ini文件相同

编辑配置文件: .ini

如何使用Python configparser模块解析配置文件步骤详解?

模板:内容自定义

一、 编辑配置文件

import configparser config = configparser.ConfigParser() config['DEFAULT'] = { 'ServerAliveInterval':'45', 'Compression':'yes', 'CompressionLevel':'9', 'ForwardX11':'yes' } config['bitbucker.org'] = { 'Host Port':'50022', 'ForwardX11':'no' } config['path'] = { 'Base_Path':'D:\python\pychrom\路飞学城\day8', 'student_path':'D:\python\pychrom\路飞学城\day8\configparser模块.py' } with open('example.ini','w',encoding='utf-8') as configfile: config.write(configfile)

二、读取配置文件

import configparser config = configparser.ConfigParser() config.read('example.ini',encoding='utf-8') print(config.sections()) # 查看分组情况,默认default是不显示的 print('bitbucker.org' in config) # Flase 判断一个组在不在这个文件当中 print('bitbucker.com' in config) # True print(config['bitbucker.org']['host_port']) # 查钊这个文件中这个分组下面有没有这个配置 print(config['bitbucker.org']['user']) # 没有就报错 for key in config['bitbucker.org']: # 取默认分组和这个组的下面所有配置 print(key) # 只能取到 key print(config.options('bitbucker.org')) # 取分组下面的配置,包括默认分组 只能取到值 print(config.items('bitbucker.org')) # 取到分组下面的键值对,包括默认分组 print(config.get('path','base_path')) # 获取某个分组下面的键来获取值

三、增删改查

import configparser config = configparser.ConfigParser() config.read('example.ini',encoding='utf-8') config.add_section('zuming') # 添加组 config.remove_section('zuming') # 删除一个组 config.remove_option('bitbucker.org','host_port') # 删除某个组中的某一项 config.set('bitbucker.org','host_port','22') # 修改某个组下面的值 config.write(open('example.ini','w',encoding='utf-8')) # 必须添加这句话才能生效

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。