gpt4 book ai didi

Python shlex.split(),忽略单引号

转载 作者:太空狗 更新时间:2023-10-29 18:17:00 30 4
gpt4 key购买 nike

在 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com