如何在Python中如何使用shlex.split()
或类似的字符串来分割字符串,仅保留双引号?例如,如果输入是"hello, world" is what 'i say'
,那么输出将是["hello, world", "is", "what", "'i", "say'"]
。
最佳答案
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'.''')
关于Python shlex.split(),忽略单引号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6868382/