Hi,聆听·彼岸



听说,你是我最遥不可及的梦。
What should I do?
What should I do?
I think I'm going to fall in love with myself.

python读写配置文件

发表于 2022-07-25| 分类于 笔记 | | 评论数

使用python3自带的configparser

import configparser
config = configparser.ConfigParser()
config.sections()#浏览所有域,返回列表
config.options("sections")#浏览sections下所有key,返回列表
config.items("sections")#浏览sections下所有key和value,返回列表
config.read('example.ini')#读取配置文件
value=config['section']['key']#获取值
value=config.get('section','key')
config.read_string("[section]\nkey=value")#用字符串更改配置
config.read_dict({"section": {"nkey": "value"}})#用字典更改配置
config['section'].get('key')#获取配置
'''
getint() = int(get())
同理
getfloat()
getboolean()
...
'''
config.add_section('section') #添加一个域
config.set('section','key','value') #域下添加一个key value对
config.write(open(path,'w')) #要使用'w'

官方文档

sections()
Return all the configuration section names, sans DEFAULT.

has_section(section)
Return whether the given section exists.

has_option(section, option)
Return whether the given option exists in the given section.

options(section)
Return list of configuration options for the named section.

read(filenames, encoding=None)
Read and parse the iterable of named configuration files, given by
name. A single filename is also allowed. Non-existing files
are ignored. Return list of successfully read files.

read_file(f, filename=None)
Read and parse one configuration file, given as a file object.
The filename defaults to f.name; it is only used in error
messages (if f has no `name' attribute, the string `<???>' is used).

read_string(string)
Read configuration from a given string.

read_dict(dictionary)
Read configuration from a dictionary. Keys are section names,
values are dictionaries with keys and values that should be present
in the section. If the used dictionary type preserves order, sections
and their keys will be added in order. Values are automatically
converted to strings.

get(section, option, raw=False, vars=None, fallback=_UNSET)
Return a string value for the named option. All % interpolations are
expanded in the return values, based on the defaults passed into the
constructor and the DEFAULT section. Additional substitutions may be
provided using the `vars' argument, which must be a dictionary whose
contents override any pre-existing defaults. If `option' is a key in
`vars', the value from `vars' is used.

getint(section, options, raw=False, vars=None, fallback=_UNSET)
Like get(), but convert value to an integer.

getfloat(section, options, raw=False, vars=None, fallback=_UNSET)
Like get(), but convert value to a float.

getboolean(section, options, raw=False, vars=None, fallback=_UNSET)
Like get(), but convert value to a boolean (currently case
insensitively defined as 0, false, no, off for False, and 1, true,
yes, on for True). Returns False or True.

items(section=_UNSET, raw=False, vars=None)
If section is given, return a list of tuples with (name, value) for
each option in the section. Otherwise, return a list of tuples with
(section_name, section_proxy) for each section, including DEFAULTSECT.

remove_section(section)
Remove the given file section and all its options.

remove_option(section, option)
Remove the given option from the given section.

set(section, option, value)
Set the given option.

write(fp, space_around_delimiters=True)
Write the configuration state in .ini format. If
`space_around_delimiters' is True (the default), delimiters
between keys and values are surrounded by spaces.

满分是10分的话,这篇文章你给几分,您的支持将鼓励我继续创作!