Cor*_*ory 5 python parsing pyparsing
我有类似于YAML的数据,需要使用Pyparsing为它创建语法.与Python一样,Yaml的数据范围由空白定义
数据:
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
Run Code Online (Sandbox Code Playgroud)
解析上面的内容之后,它应该输出类似于这样的东西:
{'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'}}}}
Run Code Online (Sandbox Code Playgroud)
[编辑]数据类似于YAML但不完全相同.所以YAML Python解析器无法解析它.我留下了一些细节,以使示例数据更简单
您可以使用PyYAML代替 Pyparsing 。
import yaml
f = open('yyy.yaml', 'r')
print yaml.load(f)
Run Code Online (Sandbox Code Playgroud)
输出:
{'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'}}}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |