简体   繁体   中英

How to split string in python using multiple delimeters

I am using Python3 and wants to split the string into the multiple substrings. My code is shown below.

pr_list=re.split(r'\s{1,}', "the string, data,could have,  multiple   spaces".strip())

Above code works fine for single or multiple spaces which comes as a delimiter. But additionally, how can I split the same string using the another delimiter ie ',' as well?

For eg

"data,could" string shown above also split into "data" and "could" string.

Out put i am looking is just the words, with out spaces or ','.

['the', 'string', 'data', 'could', 'have', 'multiple', 'spaces']

Maybe you can use this r'[,]+'

import re

pr_list=re.split(r'[ ,]+', "the string, data,could have,  multiple   spaces".strip())
>>> pr_list
['the', 'string', 'data', 'could', 'have', 'multiple', 'spaces']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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