def
split_by_ignore_quote
(
s
:
str
,
sep
:
str
,
quote_mark
=
None
,
escape_mark
=
"\\"
)
-
>
List
[
str
]
:
"""按 sep 切分字符串,并忽略被引号框柱的部分中的 sep
Parameters
----------
s : str
目标字符串
sep : str
quote_mark : Set[str], default = {"'", "\""}
escape_mark : str, default = "\\"
Returns
-------
List[str]
切分后的字符串列表
Examples
--------
>>> split_by_ignore_quote(r"1,'2,3',4", sep=",")
['1', "'2,3'", '4']
>>> split_by_ignore_quote(r"1,\\'2,3,4", sep=",")
['1', "'2", '3', '4']
>>> split_by_ignore_quote(r"1,'2,3,4", sep=",")
['1', "'2,3,4"]
if
quote_mark
is
None
:
quote_mark
=
{
"'"
,
"\""
}
assert
escape_mark
not
in
quote_mark
,
"escape mark in quote mark"
assert
sep
not
in
quote_mark
,
"separator mark in quote mark"
assert
escape_mark
!=
sep
,
"escape mark equal separator mark"
quote
=
False
escape
=
False
res
=
[
[
]
]
for
ch
in
s
:
if
escape
is
True
:
res
[
-
1
]
.
append
(
ch
)
escape
=
False
elif
ch
in
escape_mark
:
escape
=
True
else
:
if
ch
==
sep
and
quote
is
False
:
res
.
append
(
[
]
)
else
:
res
[
-
1
]
.
append
(
ch
)
if
ch
in
quote_mark
:
quote
=
not
quote
return
[
""
.
join
(
item
)
for
item
in
res
]
Python
字符串
分割
split
不拆分括号里面的
内容
欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定
内容
居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程...
python
中
字符串
的
split
能很方便的将
字符串
根据指定的分割符分割,但是如果不想分割
引号
中的分割符的话,
split
就无能为力了,写了一个小函数,
忽略
引号
中的分割符
def my_
split
(s, sep=' ', ignore='"'):
"""以seq分割,重复的seq按一个计算,
忽略
ignore中的seq,返回开始、结束这样的索引数组"""
ignore_flag