文章目录
- python 读yaml文件
- 先安装 pip install pyyaml
- 读取
- 对象
- 数组
- 复合结构
- 纯量
- 字符串
- 单引号,双引号
https://www.ruanyifeng.com/blog/2016/07/yaml.html
python 读yaml文件
先安装 pip install pyyaml
读取
importyamlwithopen("config.yaml","r",encoding="utf-8")asf:data=yaml.safe_load(f)print(data)# 读出来是 dict / list 组合对象
看到 : → 这是“字典”的一项
数组
看到 - → 这是“列表”的一个元素
复合结构
{'languages':['Ruby','Perl','Python'],'websites':{'Perl':'use.perl.org','Python':'python.org','Ruby':'ruby-lang.org','YAML':'yaml.org'}}纯量
{'isSet':True,'number':12.3,'parent':None}字符串
单引号,双引号
单引号 ‘…’:基本“原样”,不解析 \n、\t 这些转义序列(它们就当普通字符)。
例:‘内容\n字符串’ 里面的 \n 就是两个字符:反斜杠和 n。
双引号 “…”:会解析转义序列。特殊字符会输出为其本身想表达的含义
例:“内容\n字符串” 里的 \n 会变成真正的换行
{'s1':'内容\\n字符串','s2':'内容\n字符串'}{'str':"labor's day",'str1':"labor''s"}{'this':'Foo\nBar\n','that':'Foo Bar'}Foo Bar Foo Barstr:这是一段 多行 字符串this:|Foo Barthat:>Foo Bars1:|Foos2:|+ Foos3:|-Foos4:|-Foo{'s1':'Foo\n','s2':'Foo\n\n\n','s3':'Foo','s4':'Foo','str':'这是一段 多行 字符串','that':'Foo Bar\n','this':'Foo\nBar\n'}