Re: [Pyparsing] Check for tabs
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2013-10-29 04:49:19
|
One of pyparsing's default behaviors is to skip over whitespace, so when whitespace is part of your parser, you sometimes have to take some extra steps. In the case of <TAB>s, you need to override pyparsing's default of replacing tabs in the input string with spaces. Otherwise, the input string first gets run through str.expandtabs before being parsed, and the tab expressions in your grammar will never match. Here's how: word = Word(alphas) tab = White('\t', exact=1) data = word + tab + word + tab + tab + word # this step is important data.parseWithTabs() s = 'name\tdate\t\tlocation' data.parseString(s).asList() ['name', '\t', 'date', '\t', '\t', 'location'] If you leave out this call, you'll get a ParseException because your input string will have been expanded to 'name date location', with no tabs. -- Paul -----Original Message----- From: Hanchel Cheng [mailto:han...@br...] Sent: Monday, October 28, 2013 3:52 PM To: pyp...@li... Subject: [Pyparsing] Check for tabs Hello! I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has. My question is pretty basic: I have a string 'name\tdate\t\tlocation'. What would I do to ensure that between the 'name' and 'date' there is exactly one tab and between 'date' and 'location' there is exactly two tabs? Thanks, Hanchel ---------------------------------------------------------------------------- -- Android is increasing in popularity, but the open development platform that developers love is also attractive to malware creators. Download this white paper to learn more about secure code signing practices that can help keep Android apps secure. http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |