
正文
Python ymal 模块和configparser
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
ymal : 是一种config文件
# !/user/bin/python
# -*- coding: utf-8 -*-import configparser# 生成一个config文件 (当前目录下会生成一个example.ini的文件)
config = configparser.ConfigParser()config["default"] = {"ServerAliveInterval": "",
"Compression":"",
"CompressionLevel":""}config["bitbucket.org"] = {}
config["bitbucket.org"]["user"] = "hg"
config["topsecret.server.com"] = {}
topsecret = config["topsecret.server.com"]
topsecret["host port"] = ""
topsecret["portwardxii"] = "no"with open("example.ini","w") as configfile:
config.write(configfile)# 读取config 文件
conf = configparser.ConfigParser()
conf.read("example.xml")print(conf.sections()) # 打印出文件的节点, 即[ ]里的内容, 但是[default]默认是不打印的
print(conf.default_section) # 打印默认节点, 即[default]
print(conf['bitbucket.org']['user']) # 读取指定节点的内容. 为什么读取失败? TODO






