简体   繁体   English

反复在python中拆分字符串

[英]Repeatedly splitting strings in python

I am looking to make a function to break a string into a list of str by breaking it at various punctuation points (eg , ! ?) that I specify. 我正在寻找一个函数,通过在我指定的各种标点(例如,!?)处将字符串拆分为str列表。 I know I should used the .split() function with the specific punctuation, however I can't figure out how to get iterate running the split with each punctuation character specified to produce a single list of str with made up from the original str split at every punctuation character. 我知道我应该将.split()函数与特定的标点符号一起使用,但是我无法弄清楚如何使用指定的每个标点符号来迭代运行拆分,以生成由原始str拆分组成的单个str列表在每个标点符号处。

To split with multiple delimiters, you should use re.split() : 要使用多个定界符进行拆分,应使用re.split()

import re
pattern = r"[.,!?]"  # etc.
new = re.split(pattern, your_current_string)

Putting that in function form should be simple enough. 将其放在函数形式中应该足够简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM