解析一个特设的树 [英] Parsing an ad-hoc tree

查看:195
本文介绍了解析一个特设的树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的平面文件:

I have a flat file that appears as follows:

Soccer

+Team:US
++Shirt:Red & White Stripes
++Shorts:Blue
++Players:17
+++Active:11
++++Forward:2
++++Midfield:4
++++Defense:4
++++Goalkeeper:1
+++Substitute:6
++++Forward:1
++++Midfied:2
++++Defense:3
++++Goalkeeper:

+Team:Mexico
++Shirt:Green
++Shorts:White
++Players:17
+++Active:11
++++Forward:3
++++Midfield:3
++++Defense:4
++++Goalkeeper:1
+++Substitute:6
++++Forward:2
++++Midfield:1
++++Defense:2
++++Goalkeeper:1

什么是解析到这一点在Python数据结构的最有效方法是什么?或以其他方式,我将如何转换到这一点XML,JSON或纯Python对象?

What is the most efficient way to parse this into a data structure in Python? Or otherwise, how would I convert this into XML, JSON or a plain Python object?

将所得的数据结构应该能够支持某种查询,例如在伪code如:

The resulting data structure should be able to support some kind of querying, for example in psuedocode such as:

Soccer[Team='US'][Shirt]

应该能够返回红染料;白色条纹

should be able to return "Red & White Stripes"

类似东西的量级,

Soccer[Team='US'][Players][Substitute][Goalkeeper]

应该返回None

should return None

有没有办法,就可以使用此模块?

Is there a module that'll be of use here?

推荐答案

对于这样的事情,你可以使用马尔巴: :R2 ,一个的Perl 接口的马尔巴,一般的BNF语法分析器 - 的解析器解析树。一些更多的解释是在回答相关的问题<一href="http://stackoverflow.com/questions/26184812/processing-repeatedly-structured-text-file-with-python?lq=1">Processing与Python 反复的结构化文本文件

For such thing you can use Marpa::R2, a Perl interface to Marpa, a general BNF parser — parser, parse tree. Some more explanations are in my answer to related question Processing repeatedly structured text file with python

的例子ast_traverse():<一href="http://stackoverflow.com/questions/26179905/parse-values-from-a-block-of-text-based-on-specific-keys/26182278#26182278">Parse从文本的基础上的特定按键块值

这篇关于解析一个特设的树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆