########################################
# some comment
# other comment
########################################
block1 {
value=data
some_value=some other kind of data
othervalue=032423432
}
block2 {
value=data
some_value=some other kind of data
othervalue=032423432
}发布于 2009-11-01 00:05:48
最好的方法是使用现有的格式,比如JSON。
以下是针对您的格式的示例解析器:
from lepl import (AnyBut, Digit, Drop, Eos, Integer, Letter,
NON_GREEDY, Regexp, Space, Separator, Word)
# EBNF
# name = ( letter | "_" ) , { letter | "_" | digit } ;
name = Word(Letter() | '_',
Letter() | '_' | Digit())
# words = word , space+ , word , { space+ , word } ;
# two or more space-separated words (non-greedy to allow comment at the end)
words = Word()[2::NON_GREEDY, ~Space()[1:]] > list
# value = integer | word | words ;
value = (Integer() >> int) | Word() | words
# comment = "#" , { all characters - "\n" } , ( "\n" | EOF ) ;
comment = '#' & AnyBut('\n')[:] & ('\n' | Eos())
with Separator(~Regexp(r'\s*')):
# statement = name , "=" , value ;
statement = name & Drop('=') & value > tuple
# suite = "{" , { comment | statement } , "}" ;
suite = Drop('{') & (~comment | statement)[:] & Drop('}') > dict
# block = name , suite ;
block = name & suite > tuple
# config = { comment | block } ;
config = (~comment | block)[:] & Eos() > dict
from pprint import pprint
pprint(config.parse(open('input.cfg').read()))输出:
[{'block1': {'othervalue': 32423432,
'some_value': ['some', 'other', 'kind', 'of', 'data'],
'value': 'data'},
'block2': {'othervalue': 32423432,
'some_value': ['some', 'other', 'kind', 'of', 'data'],
'value': 'data'}}]发布于 2009-01-29 21:34:23
嗯,数据看起来很有规律。所以你可以这样做(未测试):
class Block(object):
def __init__(self, name):
self.name = name
infile = open(...) # insert filename here
current = None
blocks = []
for line in infile:
if line.lstrip().startswith('#'):
continue
elif line.rstrip().endswith('{'):
current = Block(line.split()[0])
elif '=' in line:
attr, value = line.strip().split('=')
try:
value = int(value)
except ValueError:
pass
setattr(current, attr, value)
elif line.rstrip().endswith('}'):
blocks.append(current)结果将是数据块实例列表,其中block.name将是名称('block1'、'block2'等)。和其他属性对应于数据中的键。因此,blocks[0].value将是'data',等等。请注意,这只将字符串和整数作为值来处理。
(如果您的键可以包含'name‘,那么这里有一个明显的bug。如果可能发生这种情况,您可能希望将self.name更改为self._name或其他值)
哈!
发布于 2009-01-29 22:24:18
如果您不是真的想要解析,而是文本处理和输入数据真的很规则,那么就使用John的解决方案。如果你真的需要一些解析(比如你得到的数据有一些更复杂的规则),那么根据你需要解析的数据量,我会选择pyparsing或simpleparse。这两个我都试过了,但实际上pyparsing对我来说太慢了。
https://stackoverflow.com/questions/493484
复制相似问题