How to tell to shlex that if the character ; is found, then, don't split anything anymore?
Example:
shlex.split("""hello "column number 2" foo ; bar baz""")
should give
["hello", "column number 2", "foo", "; bar baz"]
instead of ["hello", "column number 2", "foo", ";", "bar", "baz"].
More generally, is there a way to define "comment" separators with shlex? i.e.
shlex.split("""hello "column number 2" foo ;this is a comment; "last one" bye """)
should give
["hello", "column number 2", "foo", ";this is a comment;", "last one", "bye"]
The
shlexparser provides an option for specifying the comment character(s), but it's not available from the simplifiedshlex.splitinterface. Example:Here is a slightly expanded
splitfunction, mostly copied from the Python standard library, with a slight modification to thecommentsparameter, allowing the specification of comment characters:You might want to change the default value of
commentsin the above code; as written, it has the same default asshlex.split, which is not to recognise comments at all. (The parser objects created byshlex.shlexdefault to#as the comment character, which is what you get if you specifycomments=True. I preserved this behaviour for compatibility.)Note that comments are ignored; they do not appear in the result vector at all. When the parser hits a comment character, it just stops parsing. (So there can never be two comments.) The
commentsstring is a list of possible comments characters, not a comment sequence. So if you want to recognise both#and;as comment characters, specifycomments='#:'.Here's a sample run: