Pet*_*ons 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'.''')
Run Code Online (Sandbox Code Playgroud)
您可以使用它shlex.quotes来控制哪些字符将被视为字符串引号.你也需要修改shlex.wordchars,以保持'与i和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'"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18531 次 |
| 最近记录: |