How, in Python, can I use shlex.split()
or similar to split strings, preserving only double quotes? For example, if the input is "hello, world" is what 'i say'
then the output would be ["hello, world", "is", "what", "'i", "say'"]
.
Asked
Active
Viewed 2.3k times
10

tekknolagi
- 10,663
- 24
- 75
- 119
-
It captures the entire double quoted string, but splits up the single quoted string. – tekknolagi Jul 29 '11 at 03:43
-
1+1 for the interesting question. I don't get to write Python much these days, and I've never seen or used `shlex` before, so this was a fun one. – Matt Ball Jul 29 '11 at 04:03
-
Python is really my job description these days – tekknolagi Jul 29 '11 at 04:27
2 Answers
16
import shlex
def newSplit(value):
lex = shlex.shlex(value)
lex.quotes = '"'
lex.whitespace_split = True
lex.commenters = ''
return list(lex)
print newSplit('''This string has "some double quotes" and 'some single quotes'.''')

Peter Lyons
- 142,938
- 30
- 279
- 274
-
1Only negligibly so, and the `lex.commenters` bit is actually something that my answer doesn't do. +1 for a different way to git-r-dun. – Matt Ball Jul 29 '11 at 04:05
-
I started with the source code to the shlex.split function from the python source and just tweaked it with the list of quotes characters. – Peter Lyons Jul 29 '11 at 17:36
-
It took me a while, but this is slightly different than the normal shlex.split which passes posix ... [`shlex.shlex(value, posix=True)`](http://hg.python.org/cpython/file/2a38df26e009/Lib/shlex.py#l274) – Pykler Sep 04 '13 at 16:49
8
You can use shlex.quotes
to control which characters will be considered string quotes. You'll need to modify shlex.wordchars
as well, to keep the '
with the i
and the say
.
import shlex
input = '"hello, world" is what \'i say\''
lexer = shlex.shlex(input)
lexer.quotes = '"'
lexer.wordchars += '\''
output = list(lexer)
# ['"hello, world"', 'is', 'what', "'i", "say'"]

Matt Ball
- 354,903
- 100
- 647
- 710
-
actually, this one failed on something else I was writing, and Peter's worked. thank you though! – tekknolagi Jul 29 '11 at 15:36
-
1
-
If the escaped quote is in the quoted section, is there a way forward? Say, `s = "1 'K\^o, Suzuk\'e'"` to split into `[1, "K\^o, Suzuk\'e"]` – user14717 Jun 14 '16 at 01:48