用柯利布雷特斯解析文件

我需要用括号将信息解析为一个文件,例如:


Continent

{

Name    Europe

Country

{

Name    UK

Dog

{

Name    Fiffi

Colour  Gray

}

Dog

{

Name    Smut

Colour  Black

}

}

}

这是我在Python中尝试过的


from io import open

from pyparsing import *

import pprint


def parse(s):

    return nestedExpr('{','}').parseString(s).asList()


def test(strng):

    print strng

    try:

        cfgFile = file(strng)

        cfgData = "".join( cfgFile.readlines() )

        list = parse( cfgData )

        pp = pprint.PrettyPrinter(2)

        pp.pprint(list)


    except ParseException, err:

        print err.line

        print " "*(err.column-1) + "^"

        print err


    cfgFile.close()

    print

    return list


if __name__ == '__main__':

    test('testfile')

但这失败并显示错误:


testfile

Continent

^

Expected "{" (at char 0), (line:1, col:1)


Traceback (most recent call last):

  File "xxx.py", line 55, in <module>

    test('testfile')

  File "xxx.py", line 40, in test

    return list

UnboundLocalError: local variable 'list' referenced before assignment  

我需要做些什么才能使这项工作?是比pyparsing更好的解析器吗?


达令说
浏览 155回答 2
2回答

明月笑刀无情

递归是这里的关键。尝试一些解决方法:def parse(it):&nbsp; &nbsp; result = []&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tk = next(it)&nbsp; &nbsp; &nbsp; &nbsp; except StopIteration:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; if tk == '}':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; val = next(it)&nbsp; &nbsp; &nbsp; &nbsp; if val == '{':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append((tk,parse(it)))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append((tk, val))&nbsp; &nbsp; return result用例:import pprint&nbsp; &nbsp; &nbsp; &nbsp;data = """Continent{Name&nbsp; &nbsp; EuropeCountry{Name&nbsp; &nbsp; UKDog{Name&nbsp; &nbsp; FiffiColour&nbsp; Gray}Dog{Name&nbsp; &nbsp; SmutColour&nbsp; Black}}}"""r = parse(iter(data.split()))pprint.pprint(r)...产生(Python 2.6):[('Continent',&nbsp; [('Name', 'Europe'),&nbsp; &nbsp;('Country',&nbsp; &nbsp; [('Name', 'UK'),&nbsp; &nbsp; &nbsp;('Dog', [('Name', 'Fiffi'), ('Colour', 'Gray')]),&nbsp; &nbsp; &nbsp;('Dog', [('Name', 'Smut'), ('Colour', 'Black')])])])]请仅以此为起点,并随时根据需要改进代码(取决于数据,字典可能是更好的选择)。此外,示例代码无法处理格式错误的数据(特别是多余或丢失的数据}-我敦促您进行完整的测试;)编辑:发现后pyparsing,我尝试了以下方法,这些方法似乎可以(更好)地工作,并且可以(更)容易地针对特殊需求进行定制:import pprintfrom pyparsing import Word, Literal, Forward, Group, ZeroOrMore, alphasdef syntax():&nbsp; &nbsp; lbr = Literal( '{' ).suppress()&nbsp; &nbsp; rbr = Literal( '}' ).suppress()&nbsp; &nbsp; key = Word( alphas )&nbsp; &nbsp; atom = Word ( alphas )&nbsp; &nbsp; expr = Forward()&nbsp; &nbsp; pair = atom | (lbr + ZeroOrMore( expr ) + rbr)&nbsp; &nbsp; expr << Group ( key + pair )&nbsp; &nbsp; return exprexpr = syntax()result = expr.parseString(data).asList()pprint.pprint(result)生产:[['Continent',&nbsp; ['Name', 'Europe'],&nbsp; ['Country',&nbsp; &nbsp;['Name', 'UK'],&nbsp; &nbsp;['Dog', ['Name', 'Fiffi'], ['Colour', 'Gray']],&nbsp; &nbsp;['Dog', ['Name', 'Smut'], ['Colour', 'Black']]]]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python