前提:安装好re库
以下是在jupyter notebook中演示:
[In]
import re
a = “sa,sb,sc,sd(se ty,sff ee)hhhh,sj,sk”
sentences = re.split(r",(?![^(]*\))", a)
sentences
[out]
[‘sa’, ‘sb’, ‘sc’, ‘sd(se ty,sff ee)hhhh’, ‘sj’, ‘sk’]
以下是上面重复的内容(用优快云的代码方式显示)
[In]
a = "sa,sb,sc,sd(se ty,sff ee)hhhh,sj,sk"
sentences = re.split(r",(?![^(]*\))", a)
sentences
[out]
['sa', 'sb', 'sc', 'sd(se ty,sff ee)hhhh', 'sj', 'sk']
转自StackOverflow: https://stackoverflow.com/questions/39647555/how-to-split-string-while-ignoring-portion-in-parentheses
另外有一篇“忽略引号里的逗号”,值得借鉴。
https://blog.youkuaiyun.com/muye0503/article/details/38415709