Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Pyparsing is pretty tolerant of whitespace characters as you show here, so I'm surprised this is an issue.</p> <p>One problem that you <em>do</em> have is in your definition of <code>list_G</code>:</p> <pre><code>list_G = delimitedList(Word(printables))('mlist') #grammar for list </code></pre> <p>I get what you're trying to do here, but your list element expression has a key problem. <code>delimitedList(expr)</code> is a handy short-cut for <code>expr + ZeroOrMore(Suppress(',') + expr)</code>, but the list element expression you are using for <code>expr</code> is <code>Word(printables)</code>, a word-group of any non-whitespace character. Unfortunately, this includes ',', your list delimiter. When you pass your string "one,two,three" to this expression, <code>Word(printables)</code> will parse the whole thing before it ever looks for a delimiter:</p> <pre><code>&gt;&gt;&gt; list_G = delimitedList(Word(printables)) &gt;&gt;&gt; print (list_G.parseString("one,two,three")) ['one,two,three'] </code></pre> <p>You need a way to say "I want my word to be everything printable that is not a comma". In older versions of pyparsing, you had to do this yourself with something like:</p> <pre><code>word_of_everything_except_a_comma = Word(''.join(c for c in printables if c != ',')) </code></pre> <p>In version 1.5.6, I added an <code>excludeChars</code> argument to Word to simplify this. Now you can just write:</p> <pre><code>word_of_everything_except_a_comma = Word(printables, excludeChars=',') </code></pre> <p>With a word like this, you will get a proper 3-element list:</p> <pre><code>&gt;&gt;&gt; list_G = delimitedList(Word(printables, excludeChars=',')) &gt;&gt;&gt; print (list_G.parseString("one,two,three")) ['one', 'two', 'three'] </code></pre> <p>This may be the issue you were struggling with, as pyparsing will implicitly skip over the whitespace characters that you are stripping out:</p> <pre><code>&gt;&gt;&gt; s = "one, two\t\t,\n\n\t\t\t three" &gt;&gt;&gt; print (s) one, two , three &gt;&gt;&gt; print (list_G.parseString(s)) ['one', 'two', 'three'] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload