2014-07-14 135 views
1

我想使用pyparsing的commaSeparatedList來分隔字符串並忽略'{''}'內的人員。pyparsing,忽略不能忽略字符串內聯

例如:

a = 'xyz,abc{def,123,456}' 

解析後,我想了 [ '某某', 'ABC {閃避,123456}']

我寫了這個:

nested_expr = '{' + SkipTo('}') + '}' 
commaSeparatedList.ignore(nested_expr).parseString(a) 

結果:(['xyz','abc {def','123','456}'],{})

Actulally 它s EEMS當有前一個分隔像 '{',這將工作

a = 'xyz,abc,{def,123,456}' 
commaSeparatedList.ignore(nested_expr).parseString(a) 

結果:([ '某某', 'ABC', ''],{})

你能來看看爲什麼發生這種情況?

+0

是否有可能嵌套?如果不是,請嘗試使用'QuotedString('{',endQuoteChar ='}')'來抑制大括號內的逗號檢測。 – PaulMcG

+0

感謝您的回覆,但是這個QuotedString仍然不起作用,'a ='xyz,abc {def,123,456}'; commaSeparatedList.ignore(QuotedString('{',endQuoteChar ='}'))。parseString(a)'仍然解析'{','}' –

回答

0

開拓pyparsing.py源文件,看看commaSeparatedList是如何實現的 - 它不是那麼多,很容易地適應您的情況:

# original 
_commasepitem = Combine(OneOrMore(Word(printables, excludeChars=',') + 
            Optional(Word(" \t") + 
              ~Literal(",") + ~LineEnd()))).streamline().setName("commaItem") 
commaSeparatedList = delimitedList(Optional(quotedString.copy() | _commasepitem, default="")).setName("commaSeparatedList") 


# modified 
_commasepitem = Combine(OneOrMore(QuotedString('{',endQuoteChar='}',unquoteResults=False) | Word(printables, excludeChars=',{}') + 
            Optional(Word(" \t") + 
              ~Literal(",") + ~LineEnd()))).streamline().setName("commaItem") 

commaSeparatedList = delimitedList(Optional(_commasepitem, default="")).setName("commaSeparatedList") 

這是很重要的詞_commasepitem 允許包含「{}」字符。