zoukankan      html  css  js  c++  java
  • configparser使用

    sudo easy_install dict4ini

    ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件. 这篇文章简单介绍一下读取配置文件的方法.

    配置文件.顾名思议就是存放配置的文件.下面是个例子
    [info]
    age = 21
    name = chen
    sex = male

    其中[ ] 中的info是这段配置的名字
    下面age,name都是属性

    下面的代码演示了如何读取配置文件.和修改配置中变量的值

    from __future__ import with_statement
    import ConfigParser
    config
    =ConfigParser.ConfigParser()
    with open('testcfg.cfg','rw') as cfgfile:
        config.readfp(cfgfile)
        name
    =config.get('info','name')
        age
    =config.get('info','age')
        
    print name
        
    print age
        config.set(
    'info','sex','male')
        config.set(
    'info','age','21')
        age
    =config.get('info','age')
        
    print name
        
    print age

  • 相关阅读:
    poj1228 Grandpa's Estate
    poj1113 Wall
    poj2826 An Easy Problem?!
    poj1269 Intersecting Lines
    poj3304 Segments
    BZOJ3832Rally题解
    BZOJ2802Warehouse Store题解
    李超树详解
    BZOJ4241历史研究题解
    洛谷2050 BZOJ2897美食节题解
  • 原文地址:https://www.cnblogs.com/lexus/p/1833710.html
Copyright © 2011-2022 走看看