Python中ini配置文件读写的实现
程序员文章站
2022-06-30 20:07:23
导入模块import configparser # py3写入config = configparser.configparser()config["default"] = { 'servera...
导入模块
import configparser # py3
写入
config = configparser.configparser() config["default"] = { 'serveraliveinterval': '45', 'compression': 'yes', 'compressionlevel': '9' } config['bitbucket.org'] = {} config['bitbucket.org']['user'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['host port'] = '50022' # mutates the parser topsecret['forwardx11'] = 'no' # same here config['default']['forwardx11'] = 'yes' # 写入文件 with open('example.ini', 'w') as configfile: config.write(configfile)
读取
config = configparser.configparser() config.read("example.ini") print(config.defaults()) # ordereddict([('compression', 'yes')]) print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] print(config['bitbucket.org']['user']) # hg print(config.options("topsecret.server.com")) # ['port', 'compression'] print(config.items("topsecret.server.com")) # [('compression', 'yes'), ('port', '50022')] print(config.get("topsecret.server.com", "port")) # 50022
修改
print(config.has_section("name")) # 删除 config.remove_section("name") # 添加 config.add_section("name") config["name"]["name"] = "tom" config["name"]["asname"] = "jimi" # 设置 config.remove_option("name", "asname") config.set("name", "name", "jack") # 保存 config.write(open("example.ini", "w"))
附:ini文件
[default] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] host port = 50022 forwardx11 = no
help(configparser)
""" classes class configparser(rawconfigparser) | configparser implementing interpolation. | | add_section(self, section) | create a new section in the configuration. extends | rawconfigparser.add_section by validating if the section name is | a string. | | set(self, section, option, value=none) | set an option. extends rawconfigparser.set by validating type and | interpolation syntax on the value. | | defaults(self) | | get(self, section, option, *, raw=false, vars=none, fallback=<object object at 0x0000000002f42120>) | get an option value for a given section. | | getboolean(self, section, option, *, raw=false, vars=none, fallback=<object object at 0x0000000002f42120>) | | getfloat(self, section, option, *, raw=false, vars=none, fallback=<object object at 0x0000000002f42120>) | | getint(self, section, option, *, raw=false, vars=none, fallback=<object object at 0x0000000002f42120>) | | has_option(self, section, option) | check for the existence of a given option in a given section. | if the specified `section' is none or an empty string, default is | assumed. if the specified `section' does not exist, returns false. | | has_section(self, section) | indicate whether the named section is present in the configuration. | items(self, section=<object object at 0x0000000002f42120>, raw=false, vars=none) | return a list of (name, value) tuples for each option in a section. | | options(self, section) | return a list of option names for the given section name. | popitem(self) | remove a section from the parser and return it as | read(self, filenames, encoding=none) | read and parse a filename or a list of filenames. | return list of successfully read files. | | read_dict(self, dictionary, source='<dict>') | read configuration from a dictionary. | | read_file(self, f, source=none) | like read() but the argument must be a file-like object. | | read_string(self, string, source='<string>') | read configuration from a given string. | | readfp(self, fp, filename=none) | deprecated, use read_file instead. | | remove_option(self, section, option) | remove an option. | | remove_section(self, section) | remove a file section. | | sections(self) | return a list of section names, excluding [default] | | write(self, fp, space_around_delimiters=true) | write an .ini-format representation of the configuration state. | | clear(self) | d.clear() -> none. remove all items from d. | | pop(self, key, default=<object object at 0x0000000002f42040>) | d.pop(k[,d]) -> v, remove specified key and return the corresponding value. | if key is not found, d is returned if given, otherwise keyerror is raised. | | setdefault(self, key, default=none) | d.setdefault(k[,d]) -> d.get(k,d), also set d[k]=d if k not in d | | update(*args, **kwds) | d.update([e, ]**f) -> none. update d from mapping/iterable e and f. | if e present and has a .keys() method, does: for k in e: d[k] = e[k] | if e present and lacks .keys() method, does: for (k, v) in e: d[k] = v | in either case, this is followed by: for k, v in f.items(): d[k] = v | | keys(self) | d.keys() -> a set-like object providing a view on d's keys | | values(self) | d.values() -> an object providing a view on d's values | """
到此这篇关于python中ini配置文件读写的实现的文章就介绍到这了,更多相关python ini文件读写内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!