以逗号分隔字符串,但在括号环境中除外 [英] Split string at commas except when in bracket environment
本文介绍了以逗号分隔字符串,但在括号环境中除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用逗号分隔Python多行字符串,除非逗号在方括号内.例如字符串
I would like to split a Python multiline string at its commas, except when the commas are inside a bracketed expression. E.g., the string
{J. Doe, R. Starr}, {Lorem
{i}psum dolor }, Dol. sit., am. et.
应分为
['{J. Doe, R. Starr}', '{Lorem\n{i}psum dolor }', 'Dol. sit.', 'am. et.']
这涉及到括号匹配,因此正则表达式可能在这里没有帮助. PyParsing 具有)环境是受保护的,而不是{}
分隔的环境.
This involves bracket matching, so probably regexes are not helping out here. PyParsing has commaSeparatedList
which almost does what I need except that quoted ("
) environments are protected instead of {}
-delimited ones.
有任何提示吗?
推荐答案
编写您自己的自定义拆分功能:
Write your own custom split-function:
input_string = """{J. Doe, R. Starr}, {Lorem
{i}psum dolor }, Dol. sit., am. et."""
expected = ['{J. Doe, R. Starr}', '{Lorem\n{i}psum dolor }', 'Dol. sit.', 'am. et.']
def split(s):
parts = []
bracket_level = 0
current = []
# trick to remove special-case of trailing chars
for c in (s + ","):
if c == "," and bracket_level == 0:
parts.append("".join(current))
current = []
else:
if c == "{":
bracket_level += 1
elif c == "}":
bracket_level -= 1
current.append(c)
return parts
assert split(input_string), expected
这篇关于以逗号分隔字符串,但在括号环境中除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文