Skip to main content
 首页 » 编程设计

python之考虑报价的字符串拆分

2025年05月04日14zfyouxi

想象一下这个字符串:

"a","b","hi, this is Mboyle" 

我想用逗号分隔它,除非逗号在两个引号之间: 即:

["a","b","hi, this is Mboyle"] 

我如何实现这一目标?使用 split,“嗨,我是 Mboyle”也会被拆分!

请您参考如下方法:

你可以用","而不是逗号来分割你的字符串:

In [1]: '"a","b","hi, this is Mboyle"'.strip('"').split('","') 
Out[1]: ['a', 'b', 'hi, this is Mboyle']