我正在尝试从 yaml 文件中解析具有类似于 jinaj2 模板语法的表达式的数据,目标是删除或添加一些项目到文件中。
AddCodesList.yaml
AddCodesList:
body:
list:
{% for elt in customer %}
- code: {{ elt.code }}
name: {{ elt.name }}
country: {{ elt.country }}
{% endfor %}
result:
json:
responseCode: {{ responseCode }}
responseMsg: {{ responseMsg }}
responseData: {{ responseData }}
解析文件.py
import ruamel.yaml
from ruamel.yaml.util import load_yaml_guess_indent
data,indent,block_seq_indent=load_yaml_guess_indent(open('AddCodesList.yaml'), preserve_quotes=True)
#delete item
del data['body']['list']['code']
#add new item
data['parameters'].insert(2, 'ssl_password','xxxxxx')#create new file
ruamel.yaml.round_trip_dump(data, open('missingCode.yaml', 'w'), explicit_start=True)
执行 parseFile.py 脚本时出现以下错误:
Traceback (most recent call last):
File "d:/workspace/TEST/manageItem.py", line 4, in <module>
data, indent, block_seq_indent = load_yaml_guess_indent(open('AddCodesList.
...
if self.check_token(ValueToken):
File "C:\Python34\lib\site-packages\ruamel\yaml\scanner.py", line 1534, in ch
self.fetch_more_tokens()
File "C:\Python34\lib\site-packages\ruamel\yaml\scanner.py", line 269, in fet
% utf8(ch), self.get_mark())
ruamel.yaml.scanner.ScannerError: while scanning for the next token
found character '%' that cannot start any token
in "<unicode string>", line 4, column 6:
{% for elt in customer %}
^ (line: 4)