python - How to parse tuples that has no delimiters? -
i'm using pyparser , want parse files contain tree structure values stored after equals sign no actual delimiters otherwise.
i've done parsing except rare cases when data stored in multiple lines, have rewrite parser not stop grabbing value equals sign end of line equals sign equals sign (or end
) ignoring word preceeds (or ignoring end
).
example of data:
( itemname = foo someotherstuff = bar foo1 foo2 astring1 = itemname someotherstuff )
code:
equals = suppress("=") token = word(alphanums + "-,./_:*+=#[];") decimal = regex(r'-?0|[1-9]\d*').setparseaction(lambda t:int(t[0])) stringtemplate = token | decimal sexplist = group(suppress("(") + zeroormore(sexp) + suppress(")")) sexp = forward()
this doesn't work obviously
astring = group(stringtemplate + equals + stringtemplate)
so i've tried these:
multilinestring = group(token + equals + oneormore(stringtemplate) + ~followedby(stringtemplate + equals)) multilinestring = group(token + equals + oneormore(stringtemplate) + notany(stringtemplate + equals)) multilinestring = group(token + equals + oneormore(stringtemplate) + ~(stringtemplate + equals))
but didn't work. got either errors or parser gobbling of data 1 record.
you on right track ~followedby
, these expressions have part of oneormore
repetitive expression. like:
multilinestring = group(token + equals + oneormore(stringtemplate + ~followedby(equals))) match in multilinestring.searchstring(test): match.pprint()
some other comments:
- look @ definitions of
token
,decimal
, ,stringtemplate
. stands now, never matchdecimal
expression. avoid mistakinginteger
token
, suggest changetoken
word(alphas, alphanums + "-,./_:*+=#[];")
- you may want reconsider whether want allow '=' part of
token
. if omits space betweentoken
, delimiting '=', meant delimiter suckedtoken
key. - consider adding results names access key , value bits of each key-value pair:
multilinestring = group(token("key") + equals + oneormore(stringtemplate + ~followedby(equals))("value"))
. can access themmatch.key
,match.value
- of course can use list indexing keymatch[0]
, find named access easier work with.
Comments
Post a Comment