• python读取配置文件


    本机:python3.5

    1.需要安装configerparser 【py2是 ConfigParser】

    2.编写代码

    运行前

    #coding:utf8
    import configparser
    
    conf = configparser.ConfigParser()
    conf.read("F:\python\ClStudyDemo\Util\config.ini")
    
    # 获取指定的section,指定的option的值
    url = conf.get("global","url")
    print  (url)
    
    #获取所有的sections
    all = conf.sections()
    print(all)
    
    
    #写配置文件
    # 更新指定section,option的值
    conf.set("global","url","http://www.baidu.com1")
    #添加新的option值
    conf.set("global","classname","通信")
    # 添加新的 section
    conf.add_section("new_section")
    conf.set("new_section", "new_option", "http://www.cnblogs.com/tankxiao")
    
    # 写回配置文件
    conf.write(open("F:\python\ClStudyDemo\Util\config.ini","w"))

    运行后

    1) 基本的读取配置文件
         -read(filename) 直接读取ini文件内容
         -sections() 得到所有的section,并以列表的形式返回
         -options(section) 得到该section的所有option
         -items(section) 得到该section的所有键值对
         -get(section,option) 得到section中option的值,返回为string类型
         -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
     
    2) 基本的写入配置文件
         -add_section(section) 添加一个新的section
         -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

  • 相关阅读:
    关于大文件下载
    关于小文件下载
    小文件下载
    AppStore 中的app怎么样生成二维码,来提供下载
    重学STM32---(十)之CAN通信(二)
    重学STM32---(九)之CAN通信(一)
    将博客搬至CSDN
    绑定socket描述符到一个网络设备
    通用 Makefile(及makefile中的notdir,wildcard和patsubst)
    vsftpd 编译安装 及 隐藏版本号
  • 原文地址:https://www.cnblogs.com/dieyaxianju/p/6891282.html
Copyright © 2020-2023  润新知