最後更新: 2022-10-19
目錄
- INI files Format
- Basic Usage - ConfigParser
- Exception
- SafeConfigParser
- RawConfigParser
INI files Format
Microsoft Windows INI files
config.ini
; some comment [section1] username = dbuser password = SECRET [section2] rootpath = /home/MyEngine # some comment
* 千萬不要在 value 加上 "", i.e. username = "dbuser"
* leading whitespace is removed from values
* Comment: '#' or ';' (可以做 inline comment)
另一種格式 - "option: value"
[section1] name: value
%()s
would resolve the %(dir)s to the value of dir ("/var/log"in this case)
[MySettings] logfile=%(dir)s/log.log dir=/var/log
* "dir" 只要在同一個 section 出現就得, 不用在引用之前
Special section - DEFAULT
當自己的 section 沒有 "dir" 時, 就會用 "[DEFAULT]" 那個
[MySettings] logfile=%(dir)s/log.log #dir=/mydata/smtp_imap_loop_check [DEFAULT] dir=/var/log
Basic Usage - ConfigParser
Derived class of RawConfigParser that implements the magical interpolation feature(%()s) and
adds optional arguments to the get() and items() methods.
class:
class ConfigParser.ConfigParser([defaults[, dict_type[, allow_no_value]]])
# 拿資料方式1:
# Get an option value for the named section.
# All the '%' interpolations are expanded in the return values, unless the raw argument is true.
ConfigParser.get(section, option[, raw[, vars]])
# 拿資料方式2:
# Return a list of (name, value) pairs
ConfigParser.items(section)
Example:
import ConfigParser config = ConfigParser.ConfigParser() # Returns list of successfully parsed filenames. # 我地只個 config read 一個 ini, 不是要 return config.read("/etc/config.ini") # Get username = config.get('login', 'username') password = config.get('login', 'password') # Return a list of (name, value) pairs for each option in the given section. All_settings = ConfigParser.items('login')
Exception
- ConfigParser.Error
- ConfigParser.NoSectionError
- ConfigParser.NoOptionError
- ConfigParser.ParsingError
- ConfigParser.InterpolationError
SafeConfigParser
* SafeConfigParser 才有 set
* option values must be strings
SafeConfigParser.set(section, option, value)
config = ConfigParser.SafeConfigParser() config.set('default', 'session_id', '123456') with open(configfile, 'wb') as cfgfile: config.write(cfgfile) cfgfile.close
RawConfigParser
get
- RawConfigParser.getint(section, option)
- RawConfigParser.getfloat(section, option) # raises an exception if the value is not a float
- RawConfigParser.getboolean(section, option) # "1", "yes", "true", and "on"
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('example.cfg')
# Return a list of the sections available; DEFAULT is not included in the list.
all_sections = Config.sections()
# check 有無
RawConfigParser.has_section("section1")
RawConfigParser.has_option("section", "option")
# getfloat() raises an exception if the value is not a float
# getint() and getboolean() also do this for their respective types
a_float = config.getfloat('Section1', 'a_float')
an_int = config.getint('Section1', 'an_int')
print a_float + an_int
# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
# This is because we are using a RawConfigParser().
if config.getboolean('Section1', 'a_bool'):
print config.get('Section1', 'foo')
建立
import ConfigParser config = ConfigParser.RawConfigParser() config.add_section('Section1') config.set('Section1', 'an_int', '15') config.set('Section1', 'a_bool', 'true') config.set('Section1', 'a_float', '3.1415') config.set('Section1', 'bar', 'Python') with open('example.cfg', 'wb') as configfile: config.write(configfile)