在PyParsing中,从解析结果中删除所有引号

时间:2017-10-17 09:18:23

标签: python pyparsing

我想解析一个以逗号分隔的双引号字符串列表的字符串。起初(在In [36]: expression = delimitedList(dblQuotedString, delim=",") In [37]: expression.parseString('"foo","bar"') Out[37]: (['"foo"', '"bar"'], {}) 之后)我尝试了这个:

addParseAction(removeQuotes)

这会生成预期的匹配,但不会删除引号。所以我尝试添加In [34]: expression = delimitedList(dblQuotedString, delim=",").addParseAction(r ...: emoveQuotes) In [35]: expression.parseString('"foo","bar"') Out[35]: (['foo'], {})

'foo'

这会删除'bar'上的引号,但现在缺少(['foo', 'bar'], {})结果。如何使表达式使结果为{{1}}?

1 个答案:

答案 0 :(得分:0)

我发现我可以使用QuotedString对象,quoteChar设置为"。此对象自动删除引号:

In [40]: expression = delimitedList(QuotedString(quoteChar='"'), delim=',')

In [41]: expression.parseString('"foo","bar"')
Out[41]: (['foo', 'bar'], {})