user1806566
user1806566

Reputation: 1241

In python, is there a way to use shlex to split a line but leave quotes intact?

I know there have been similar questions about shlex, but none that seem to directly address this issue.

I would like to do basically what shlex.split() does (in POSIX mode), except just split - don't remove any quotes or backslashes. I would still like quotes and backslashes to function as they normally do (including being able to escape quotes) - I just don't want them removed.

An earlier version of my application was in perl, where Text::ParseWords is a rough equivalent of shlex. I'm looking for the python equivalent of Text::ParseWords::quotewords with $keep=1.

For instance, I would like:

shlex.split('abc "def ghi" jkl')

to return:

[ 'abc', '"def ghi"', 'jkl' ]

I recognize that if I set posix=False, this particular example would work, but that's not a general solution because posix=False also disables escaping quotes and affects lexing in other undesirable ways.

From what I can tell in the documentation, whether I use split() or a lower-level interface to the parser, if I use posix, the quotes are stripped before I see them, but if I don't, then escapes aren't handled.

I really am doing POSIX-style shell processing (which is what leads me to shlex), but shlex goes one step too far for me - stripping quotes loses the information of how the original was quoted, which is important for shell-like processing, so I want it to stop before it does that.

If there right answer isn't shlex, is there something else that doesn't involve writing a lexer from scratch?

Upvotes: 0

Views: 66

Answers (0)

Related Questions