2

我有一个具有非标准文件格式的项目,例如:

var foo = 5
load 'filename.txt'
var bar = 6
list baz = [1, 2, 3, 4]

我想把它解析成一个数据结构,就像 BeautifulSoup 所做的那样。但是 BeautifulSoup 不支持这种格式。构建解析树的pythonic方法是什么,以便我可以修改值并重新写出来?最后,我想做类似的事情:

data = parse_file('file.txt')
data.foo = data.foo * 2
data.write_file('file_new.txt')
4

2 回答 2

5

这是一个使用 pyparsing 的解决方案......它适用于您的情况。请注意,我不是专家,因此根据您的标准,代码可能很难看...干杯

class ConfigFile (dict):
    """
    Configuration file data
    """

    def __init__ (self, filename):
        """
        Parses config file.
        """

        from pyparsing import Suppress, Word, alphas, alphanums, nums, \
            delimitedList, restOfLine, printables, ZeroOrMore, Group, \
            Combine

        equal = Suppress ("=")
        lbrack = Suppress ("[")
        rbrack = Suppress ("]")
        delim = Suppress ("'")

        string = Word (printables, excludeChars = "'")
        identifier = Word (alphas, alphanums + '_')

        integer = Word (nums).setParseAction (lambda t: int (t[0]))
        real = Combine( Word(nums) + '.' + Word(nums) ).setParseAction (lambda t: float(t[0]))
        value = real | integer

        var_kwd = Suppress ("var")        
        load_kwd = Suppress ("load")
        list_kwd = Suppress ("list")            

        var_stm = Group (var_kwd + identifier + equal + value +
                         restOfLine.suppress ()).setParseAction (
                             lambda tok: tok[0].insert(len(tok[0]), 0))

        load_stm = Group (load_kwd + delim + string + delim +
                          restOfLine.suppress ()).setParseAction (
                              lambda tok: tok[0].insert(len(tok[0]), 1))

        list_stm = Group (list_kwd + identifier + equal + lbrack +
                          Group ( delimitedList (value, ",") ) +
                          rbrack + restOfLine.suppress ()).setParseAction (
                              lambda tok: tok[0].insert(len(tok[0]), 2))


        cnf_file = ZeroOrMore (var_stm | load_stm | list_stm)

        lines = cnf_file.parseFile (filename)
        self._lines = []
        for line in lines:
            self._lines.append ((line[-1], line[0]))
            if line[-1] != 1: dict.__setitem__(self, line[0], line[1])            
        self.__initialized = True
        # after initialisation, setting attributes is the same as setting an item

    def __getattr__ (self, key):
        try:
            return dict.__getitem__ (self, key)
        except KeyError:
            return None


    def __setattr__ (self, key, value):
        """Maps attributes to values. Only if we are initialised"""

        # this test allows attributes to be set in the __init__ method
        if not self.__dict__.has_key ('_ConfigFile__initialized'):
            return dict.__setattr__(self, key, value)

        # any normal attributes are handled normally
        elif self.__dict__.has_key (key): 
            dict.__setattr__(self, key, value)

        # takes care of including new 'load' statements
        elif key == 'load':
            if not isinstance (value, str):
                raise ValueError, "Invalid data type"
            self._lines.append ((1, value))

        # this is called when setting new attributes after __init__
        else:
            if not isinstance (value, int) and \
                not isinstance (value, float) and \
                not isinstance (value, list):
                raise ValueError, "Invalid data type"

            if dict.has_key (self, key):
                if type(dict.__getitem__(self, key)) != type (value):
                    raise ValueError, "Cannot modify data type."
            elif not isinstance (value, list): self._lines.append ((0, key))
            else: self._lines.append ((2, key))            
            dict.__setitem__(self, key, value)


    def Write (self, filename):
        """
        Write config file.
        """
        fid = open (filename, 'w')
        for d in self._lines:
            if d[0] == 0: fid.write ("var %s = %s\n" % (d[1], str(dict.__getitem__(self, d[1]))))
            elif d[0] == 1: fid.write ("file '%s'\n" % (d[1]))
            else: fid.write ("list %s = %s\n" % (d[1], str(dict.__getitem__(self, d[1]))))


if __name__ == "__main__":

    input="""var foo = 5
load 'filename.txt'
var bar = 6
list baz = [1, 2, 3, 4]"""

    file ("test.txt", 'w').write (input)
    config = ConfigFile ("test.txt")
    # Modify existent items
    config.foo = config.foo * 2
    # Add new items
    config.foo2 = [4,5,6,7]
    config.foo3 = 12.3456
    config.load = 'filenameX.txt'
    config.load = 'filenameXX.txt'
    config.Write ("test_new.txt")

编辑

我已经修改了要使用的类

__getitem__, __setitem__

根据我们的海报要求,模仿“访问成员”语法来解析项目的方法。享受!

附言

超载的

__setitem__

方法应该小心避免设置“正常”属性(类成员)和解析的项目(类似于属性的访问)之间的干扰。现在修复了代码以避免这些问题。有关详细信息,请参阅以下参考 http://code.activestate.com/recipes/389916/。发现这一点很有趣!

于 2012-05-31T18:33:41.970 回答
1

您拥有的是需要解析的自定义语言。

使用 Python 的众多现有解析库之一。我个人推荐PLY。或者,Pyparsing也很好并且被广泛使用和支持。

如果你的语言比较简单,你也可以实现一个手写的解析器。这是一个例子

于 2012-05-31T15:52:44.200 回答