如何从pyparsing令牌中恢复原始文本

时间:2013-06-30 21:51:29

标签: python pyparsing

我有一个name(sum(value1,sum(value2,value3)), "sumname")形式的文本,pyparsing返回适当的令牌,但是,我有兴趣收回真实的文本,我找不到。

我已经尝试使用函数setParseAction,但由于它只返回字符串和位置,我无法处理尾随部分。 就像,我只会得到:

"sum(value2,value3)), "sumname")"
"sum(value1,sum(value2,value3)), "sumname")"
"name(sum(value1,sum(value2,value3)), "sumname")"

这并不理想,我不想手动重新分析字符串以获取实际的原始字符串。

我尝试的方式是:

tokens = grammar.parseString(target_string)  
print >>sys.stderr, pyparsing.originalTextFor(tokens)

但这不起作用:

AttributeError: 'NoneType' object has no attribute 'setParseAction'

2 个答案:

答案 0 :(得分:3)

将表达式包裹在pyparsing helper originalTextFor中。

from pyparsing import makeHTMLTags, originalTextFor

sample = '<tag attr1="A1" attr2="B3">'

openTag = makeHTMLTags('tag')[0]

# the expression returned by makeHTMLTags parses the tag and 
# attributes into a list (along with a series of helpful 
# results names)
print (openTag.parseString(sample).asList())

# prints
# ['tag', ['attr1', 'A1'], ['attr2', 'B3'], False]

# wrap in 'originalTextFor' to get back the original source text
print (originalTextFor(openTag).parseString(sample).asList())

# prints
# ['<tag attr1="A1" attr2="B3">']

答案 1 :(得分:0)

根据您要通过获取原始匹配文字而尝试完成的内容,您可以使用scanStringtransformString找到更好的解决方案:

from pyparsing import makeHTMLTags, replaceWith

sample = '<other><div></div><tag attr1="A1" attr2="B3"><something>'
openTag = makeHTMLTags('tag')[0]

# grammar.scanString is a generator, yielding tokens,start,end tuples
# from the start:end values you can slice the original text from the
# source string
for tokens,start,end in openTag.scanString(sample):
    print tokens.dump()
    print sample[start:end]

# if your goal in getting the original data is to do some kind of string
# replacement, use transformString - here we convert all <TAG> tags to <REPLACE> tags
print openTag.setParseAction(replaceWith("<REPLACE>")).transformString(sample)

打印:

['tag', ['attr1', 'A1'], ['attr2', 'B3'], False]
- attr1: A1
- attr2: B3
- empty: False
- startTag: ['tag', ['attr1', 'A1'], ['attr2', 'B3'], False]
  - attr1: A1
  - attr2: B3
  - empty: False
  - tag: tag
- tag: tag
<tag attr1="A1" attr2="B3">
<other><div></div><REPLACE><something>