python - how can I get the token instead of string? -


i have following syntax:

kv = word(alphanums + '_') | quotedstring('"', escquote="\\") | quotedstring("'", escquote="\\") kv = kv.setresultsname('literal', listallmatches=true) cskv = kv + optional(zeroormore(suppress(',') + kv))  # comma separated kv 

and example:

>>> res=cskv.parsestring('a,b,c,d,e') >>> res (['a', 'b', 'c', 'd', 'e'], {'literal': [('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)]}) >>> res[0] 'a' >>> type(res[0]) <type 'str'> 

notice:

>>> type(res[0]) <type 'str'> 

i want parseresults, instead of string, such can res[0].getname() it, , should literal.

the second question how can index of token?

let's want know index of literal d, should return 3.

it's trivial in example since have 1 type of token, in problem, need know relative position of different types of tokens while processing.

any way achieve these?

edit:

i don't know why question confusing , why people keep focusing on example instead of question. following more clarification:

i use pyparsing tokenizer, tokenize string interprete it, problem have, need know token is, let's (which means it's made not-worthy-focusing-on illustration):

variable = string 

if language support this, , knows when string happens left value it's variable, if it's right value string(don't ask me why that, it's made up).

so expecting syntax:

expr = word(alphanums+'_')('leftval') + '=' + word(alphanums+'_')('rightval') 

at end, expression should tokenized:

in [3]: res = expr.parsestring('variable = string')  in [4]: res out[4]: (['variable', '=', 'string'], {'rightval': [('string', 2)], 'leftval': [('variable', 0)]}) 

but question:

in [5]: res[0] out[5]: 'variable'  in [6]: type(res[0]) out[6]: str 

now res gives string me. need tokenizer work, including information following token, lost token's name, should leftval.

funny enough, __repr__ of res has shown info need:

the token names, , in of them, captured tokens there position in tokenizing result res.

see that?

this question. not how deal comma separate values pyparsing.

according pyparsing documentation exists delimitedlist function it's used in order parse specific string composed multiple characters delimited specific separator.

i'll quote it:

helper define delimited list of expressions - delimiter defaults ','. default, list elements , delimiters can have intervening whitespace, , comments, can overridden passing combine=true in constructor. if combine set true, matching tokens returned single token string, delimiters included; otherwise, matching tokens returned list of tokens, delimiters suppressed.

now need use , parse string using parseresult object:

csvexpr = delimitedlist()  parsed = csvexpr.parsestring("a,b,c,d,e")  print(type(parsed)) # output: <class 'pyparsing.parseresults'> 

Popular posts from this blog