如何使用PyParsing提取可变长度列表

时间:2016-04-15 18:05:23

标签: pyparsing

我有以下数据:

var jsonStr = '{"date_price": [{ "date": "Jan 2000", "price": 1394.46 },{"date": "Feb 2000",      "price": 1366.42},{"date": "Mar 2000","price": 1498.58},{"date": "Apr 2000","price": 1452.43}]}',
    jsonObj = JSON.parse(jsonStr);

//var transformation = jsonObj["date_price"]; // to get a nested array

var transformation = jsonObj["date_price"].map(function(v){ return v; }); // to perform test artificial loop
    
document.write("<pre>"+ JSON.stringify(transformation, 0, 4) + "</pre>");

我想拉出&#34;协议&#34;将数据字符串转换为[&#39; dhcp&#39;,&#39;其他&#39;&#39;项目&#39;]的数组,然后将以下行解析为单独的内容。我已经尝试了protocol:: DHCP Other items following:: line ,但这也是在吃掉以下几行。

我尝试了几种不同的变体,但没有任何效果。有没有一种首选的方法呢?

例如:

'protocol::' + OneOrMore(Word(alphas))

打印:

text = """
protocol:: DHCP some other thing
following:: line
"""

delim = '::'
proto_line = Group('protocol' + delim + OneOrMore(Word(alphanums)))
following_line = Group('following' + delim + OneOrMore(Word(alphanums)))
grammar = OneOrMore(proto_line | following_line)
print grammar.parseString(text).dump()

我希望[['DHCP', 'some', 'other', 'thing', 'following']] [0]: ['DHCP', 'some', 'other', 'thing', 'following']

0 个答案:

没有答案