shlex.split()
Python では、二重引用符のみを保持して、文字列を分割するために使用または同様にするにはどうすればよいですか? たとえば、入力が の"hello, world" is what 'i say'
場合、出力は になります["hello, world", "is", "what", "'i", "say'"]
。
21023 次
2 に答える
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'.''')
于 2011-07-29T03:52:14.543 に答える
8
を使用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'"]
于 2011-07-29T03:45:02.037 に答える