R
R
Ruslan2021-01-13 10:45:24
Python
Ruslan, 2021-01-13 10:45:24

How to rename the search result for a token in pyparser?

Help me solve a simple problem, you need to rename the search result to another word, here is an example:

test_string = 'Hello, Мир'
    tokens = pp.Word(pp.alphas).setResultsName('Привет') + pp.Word(',').suppress() + pp.restOfLine()
    pprint(tokens.parseString(test_string).asList())

Result
['Hello', 'World']

Expected result
['Hello World']


Of course, I can solve this through setParseAction , but this is not feng shui:
tokens = pp.Word(pp.alphas).setParseAction(lambda t: 'Привет') + pp.Word(',').suppress() + pp.restOfLine()


There is a feeling that setResultsName is not quite for this, how then to solve the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question