python - Parsing configuration file using pyparsing -


i trying use pyparsing parse configuration file of following form

x = "/user/test" y = 3 

here code snippet

parserelement.defaultwhitespacechars = (" \t") end = stringend() nl = lineend().suppress() assignment = literal('=')  key_str = charsnotin("=") value_str = group(~assignment + restofline)  line = group(key_str + assignment + value_str) lines = zeroormore(line) lines.ignore(nl)  text = """ y = 3 x = 2 """ 

the output parsefile tells me parsing first line only. can please me find out doing wrong?

it looks on right track. maybe doing wrong when acutally pass text grammar. adding following line in code

print lines.parsestring(text) 

gives expected output

[['y ', '=', [' 3']], ['x ', '=', [' 2']]] 

as aside, don't want keep whitespace when parsing. tokens thing matters. how parse example:

eol = lineend().suppress() eq  = literal("=").suppress() val = word(alphanums) line = group(val('key') + eq + val('val') + eol) grammar = oneormore(line)  x in grammar.parsestring(text):     print x.dump() 

the output in case nicer

['y', '3'] - key: y - val: 3 ['x', '2'] - key: x - val: 2 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -