jinaj2 テンプレート構文に似た式を持つ yaml ファイルからデータを解析しようとしています。目標は、ファイルにいくつかの項目を削除または追加することです。
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 }}
parseFile.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)