天津市南开区网站开发有限公司,美食网站需求分析,莱州网页,wordpress主题 侧边栏宽度文章目录 对比INIJSONYAMLXMLTOML参考文献 对比
格式优点缺点是否支持注释INI简单易懂语言内置支持不支持复杂数据结构✓JSON支持复杂数据结构阅读起来不够直观YAML简洁有序支持复杂数据结构灵活但有歧义不同实现有兼容性问题✓XML支持复杂数据结构和命名空间语法冗长体积较大… 文章目录 对比INIJSONYAMLXMLTOML参考文献 对比
格式优点缺点是否支持注释INI简单易懂语言内置支持不支持复杂数据结构✓JSON支持复杂数据结构阅读起来不够直观×YAML简洁有序支持复杂数据结构灵活但有歧义不同实现有兼容性问题✓XML支持复杂数据结构和命名空间语法冗长体积较大✓TOML语法简洁支持复杂数据结构相对较新✓ INI
键值分隔符为 或 :
注释符为 # 或 ;
缺乏统一的标准不能表示复杂数据结构常用于 Windows 程序
config.ini
[Simple Values]
keyvalue
spaces in keysallowed
spaces in valuesallowed as well
spaces around the delimiter obviously
you can also use : to delimit keys from values[All Values Are Strings]
values like this: 1000000
or this: 3.14159265359
are they treated as numbers? : no
integers, floats and booleans are held as: strings
can use the API to get converted values directly: true[Multiline Values]
chorus: Im a lumberjack, and Im okayI sleep all night and I work all day[No Values]
key_without_value
empty string value here [You can use comments]
# like this
; or this# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.[Sections Can Be Indented]can_values_be_as_well Truedoes_that_mean_anything_special Falsepurpose formatting for readabilitymultiline_values arehandled just fine aslong as they are indenteddeeper than the first lineof a value# Did I mention we can indent comments, too?import configparserconfig configparser.ConfigParser(allow_no_valueTrue)
config.read(config.ini, encodingutf-8)for section in config.sections():print(section)for key, value in config.items(section):print( {} {}.format(key, value))特殊字符如 % 会报错 configparser.InterpolationSyntaxError: % must be followed by % or (, found: %xxxx
可以用 %% 或者 config.get(MYSQL, PASSWORD, rawTrue) JSON
常用于 JavaScript、Python、Node.js、Ruby
config.json
{mysql: {host: 127.0.0.1,user: root,password: 123456,port: 3306,database: mysql}
}import jsonconfig json.load(open(config.json))
print(config)YAML
YAML 官网
注释符为 #
常用于 Python、Java、Go 等
config.yaml
language: python
mysql:host: localhostport: 3306username: userpassword: secretfields:- id- name- age安装
pip install PyYAMLimport yamlconfig yaml.safe_load(open(config.yaml, r, encodingutf-8))
print(config)XML
注释符为 !-- 结合 --
常用于 Java、C#、.NET、Scala 等
configdatabasehostlocalhost/hostport3306/portusernameuser/usernamepasswordsecret/password/database
/configimport xml.etree.ElementTree as ETtree ET.parse(config.xml)
root tree.getroot()
config {}
for child in root:config[child.tag] {subchild.tag: subchild.text for subchild in child}
print(config)TOML
注释符为 #
常用于 Python、Rust、Go 等
config.toml
# This is a TOML documenttitle TOML Example[owner]
name Tom Preston-Werner
dob 1979-05-27T07:32:00-08:00[database]
enabled true
ports [ 8000, 8001, 8002 ]
data [ [delta, phi], [3.14] ]
temp_targets { cpu 79.5, case 72.0 }[servers][servers.alpha]
ip 10.0.0.1
role frontend[servers.beta]
ip 10.0.0.2
role backendPython 3.11 标准库开始有 tomllib
import tomllibconfig tomllib.load(open(config.toml, rb))
print(config)其余版本使用 toml
pip install tomlimport tomlconfig toml.load(open(config.toml, r))
print(config)参考文献
configparser — Python 文档json — Python 文档PyYAML Documentationxml.etree.ElementTree — Python 文档tomllib — Python 文档常见配置文件格式给力Python配置文件这一篇就够了 文章转载自: http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.chrbp.cn.gov.cn.chrbp.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.pcqxr.cn.gov.cn.pcqxr.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.gqddl.cn.gov.cn.gqddl.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn