在 Python 中使用多个分隔符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4998629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string with multiple delimiters in Python
提问by gt565k
I found some answers online, but I have no experience with regular expressions, which I believe is what is needed here.
我在网上找到了一些答案,但我没有使用正则表达式的经验,我相信这是这里需要的。
I have a string that needs to be split by either a ';' or ', ' That is, it has to be either a semicolon or a comma followed by a space. Individual commas without trailing spaces should be left untouched
我有一个字符串需要用 ';' 分割 或 ', ' 也就是说,它必须是分号或逗号后跟一个空格。没有尾随空格的单个逗号应该保持不变
Example string:
示例字符串:
"b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3], mesitylene [000108-67-8]; polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]"
should be split into a list containing the following:
应拆分为包含以下内容的列表:
('b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3]' , 'mesitylene [000108-67-8]', 'polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]')
采纳答案by Jonathan
Luckily, Python has this built-in :)
幸运的是,Python 内置了这个 :)
import re
re.split('; |, ',str)
Update:
Following your comment:
更新:
按照您的评论:
>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']
回答by Joe
Do a str.replace('; ', ', ')and then a str.split(', ')
做一个str.replace('; ', ', ')然后一个str.split(', ')
回答by Jochen Ritzel
This is how the regex look like:
这是正则表达式的样子:
import re
# "semicolon or (a comma followed by a space)"
pattern = re.compile(r";|, ")
# "(semicolon or a comma) followed by a space"
pattern = re.compile(r"[;,] ")
print pattern.split(text)
回答by Kos
Here's a safe way for any iterable of delimiters, using regular expressions:
这是使用正则表达式的任何可迭代分隔符的安全方法:
>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "stackoverflow (c) is awesome... isn't it?"
>>> regexPattern = '|'.join(map(re.escape, delimiters))
>>> regexPattern
'a|\.\.\.|\(c\)'
>>> re.split(regexPattern, example)
['st', 'ckoverflow ', ' is ', 'wesome', " isn't it?"]
re.escapeallows to build the pattern automatically and have the delimiters escaped nicely.
re.escape允许自动构建模式并很好地转义分隔符。
Here's this solution as a function for your copy-pasting pleasure:
这是作为您复制粘贴乐趣的功能的解决方案:
def split(delimiters, string, maxsplit=0):
import re
regexPattern = '|'.join(map(re.escape, delimiters))
return re.split(regexPattern, string, maxsplit)
If you're going to split often using the same delimiters, compile your regular expression beforehand like described and use RegexObject.split.
如果您要经常使用相同的分隔符进行拆分,请像描述的那样预先编译正则表达式并使用RegexObject.split.
回答by Paul
In response to Jonathan's answer above, this only seems to work for certain delimiters. For example:
针对上面乔纳森的回答,这似乎只适用于某些分隔符。例如:
>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']
>>> b='1999-05-03 10:37:00'
>>> re.split('- :', b)
['1999-05-03 10:37:00']
By putting the delimiters in square brackets it seems to work more effectively.
通过将分隔符放在方括号中,它似乎更有效地工作。
>>> re.split('[- :]', b)
['1999', '05', '03', '10', '37', '00']

