• python ConfigParser模块讲解


    configParser 模块用于操作配置文件

    注:Parser汉译为“解析”之意。

    配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

    为了更好的理解本文,我们先了解一下配置文件的组成及命名:配置文件(INI文件)由节(section)、键、值组成。

    样例配置文件example.ini

    [plain] view plain copy
     
    1. [book]  
    2. title:ConfigParser模块教程  
    3. time:2012-09-20 22:04:55  
    4.   
    5. [size]  
    6. size:1024  
    7.   
    8. [other]  
    9. blog:csdn.net  

    上面配置文件中用的是冒号,也可以用等号。

    example.py代码

    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import ConfigParser  
    3. import string  
    4. config=ConfigParser.ConfigParser()  
    5. config.read(u'd:/百度网盘/android/Python/python_example/sample.ini')  
    6. print string.upper(config.get("book","title")),  
    7. print "by",config.get("book","author"),  
    8. print "("+config.get("book","email")+")"  
    9. print  
    10. print config.get("size","size")  
    11. print  
    12. print config.sections()  
    13.   
    14. for section in config.sections():  
    15.     print section  
    16.     for option in config.options(section):  
    17.         print " ",option,"=",config.get(section,option)  

    example.py执行结果

    [plain] view plain copy
     
    1. C:\Documents and Settings\Administrator>tmp.py  
    2. CONFIGPARSER模块教程 by 大头爸爸 (366500050@qq.com)  
    3.   
    4. 1024  
    5.   
    6. ['book', 'size', 'other']  
    7. book  
    8.   title = ConfigParser模块教程  
    9.   author = 大头爸爸  
    10.   email = 366500050@qq.com  
    11.   time = 2012-09-20 22:04:55  
    12. size  
    13.   size = 1024  
    14. other  
    15.   blog = csdn.net  

    写配置文件实例

    [python] view plain copy
     
    1. import ConfigParser  
    2. import sys  
    3. config=ConfigParser.ConfigParser()  
    4. config.add_section("book")  
    5. config.set("book","title","这是标题")  
    6. config.set("book","author","大头爸爸")  
    7. config.add_section("size")  
    8. config.set("size","size",1024)  
    9. config.write(sys.stdout)  

    执行结果

    [plain] view plain copy
     
    1. [book]  
    2. title = 这是标题  
    3. author = 大头爸爸  
    4.   
    5. [size]  
    6. size = 1024  

    ConfigParser方法

    [plain] view plain copy
     
    1. 1、config=ConfigParser.ConfigParser()  
    2. 创建ConfigParser实例  
    3.   
    4. 2、config.sections()  
    5. 返回配置文件中节序列  
    6.   
    7. 3、config.options(section)  
    8. 返回某个项目中的所有键的序列  
    9.   
    10. 4、config.get(section,option)  
    11. 返回section节中,option的键值  
    12.   
    13. 5、config.add_section(str)  
    14. 添加一个配置文件节点(str)  
    15.   
    16. 6、config.set(section,option,val)  
    17. 设置section节点中,键名为option的值(val)  
    18.   
    19. 7、config.read(filename)  
    20. 读取配置文件  
    21.   
    22. 8、config.write(obj_file)  
    23. 写入配置文件  

    综合实例

    [python] view plain copy
     
    1. #coding=utf-8  
    2.   
    3. import ConfigParser  
    4.   
    5. def writeConfig(filename):  
    6.     config = ConfigParser.ConfigParser()  
    7.     # set db  
    8.     section_name = 'db'  
    9.     config.add_section( section_name )  
    10.     config.set( section_name, 'dbname', 'MySQL')  
    11.     config.set( section_name, 'host', '127.0.0.1')  
    12.     config.set( section_name, 'port', '80')  
    13.     config.set( section_name, 'password', '123456')  
    14.     config.set( section_name, 'databasename', 'test')  
    15.   
    16.     # set app  
    17.     section_name = 'app'  
    18.     config.add_section( section_name )  
    19.     config.set( section_name, 'loggerapp', '192.168.20.2')  
    20.     config.set( section_name, 'reportapp', '192.168.20.3')  
    21.   
    22.     # write to file  
    23.     config.write( open(filename, 'a') )  
    24.   
    25. def updateConfig(filename, section, **keyv):  
    26.     config = ConfigParser.ConfigParser()  
    27.     config.read(filename)  
    28.     print config.sections()  
    29.     for section in config.sections():  
    30.         print "[",section,"]"  
    31.         items = config.items(section)  
    32.         for item in items:  
    33.             print "\t",item[0]," = ",item[1]  
    34.     print config.has_option("dbname", "MySQL")  
    35.     print config.set("db", "dbname", "11")  
    36.     print "..............."  
    37.     for key in keyv:  
    38.         print "\t",key," = ", keyv[key]  
    39.     config.write( open(filename, 'r+') )  
    40.   
    41. if __name__ == '__main__':  
    42.     file_name = 'test.ini'  
    43.     writeConfig(file_name)  
    44.     updateConfig(file_name, 'app', reportapp = '192.168.100.100')  
    45.     print "end__"  
  • 相关阅读:
    GridView简单应用
    利用Field获取图片
    css的三种书写方式
    css选择器概述
    关于Object数组强转成Integer数组的问题:Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
    easyUI按钮图表对照大全
    jquery绑定点击事件的三种写法
    css中关于table的相关设置
    Leetcode 287. 寻找重复数
    LeetCode 278. 第一个错误的版本
  • 原文地址:https://www.cnblogs.com/shellshell/p/6947049.html
Copyright © 2020-2023  润新知