[英]How to create a grammar to the following data using Pyparsing
我有類似於 YAML 的數據,需要使用 Pyparsing 為其創建語法。 和 Python 一樣,Yaml 的數據 scope 是由空格定義的
數據:
object : object_name
comment : this object is created first
methods:
method_name:
input:
arg1: arg_type
arg2: arg2_type
output:
methond2_name:
input:
output:
arg1 : arg_type
解析完上面的內容后,它應該是 output 類似這樣的東西:
{'comment': 'this object is created first',
'object': 'object_name',
'methods': {'method_name': {'input': {'arg1': 'arg_type', 'arg2': 'arg2_type'},
'output': None}, 'methond2_name': {'input': None, 'output': {'arg1': 'arg_type'}}}}
[編輯] 數據類似於 YAML 但不完全相同。 所以 YAML Python 解析器無法解析它。 我留下了一些細節以使示例數據更簡單
您可以為此使用PyYAML而不是 Pyparsing。
import yaml
f = open('yyy.yaml', 'r')
print yaml.load(f)
output:
{'comment': 'this object is created first',
'object': 'object_name',
'methods': {'method_name': {'input': {'arg1': 'arg_type', 'arg2': 'arg2_type'},
'output': None}, 'methond2_name': {'input': None, 'output': {'arg1': 'arg_type'}}}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.