splitting a string with multiple delimiters doesn't work

Question:

I’m trying to split a string suing multiple delimiters. Somehow it just doesn’t work, it only works if I use each delimiter separately. What am I doing wrong?

string = 'rnrn rnrnfirst_col: col1 rnrnsecond_col: col2 rnrnthird_col: col3 rnrnfourth_col: col4 rnrnfith_col: col5rn'
splitter = "rnrn | rnrn| "
string.split(splitter)
Asked By: corianne1234

||

Answers:

Use re.split() to split the string according to a regex pattern:

import re

string = 'rnrn rnrnfirst_col: col1 rnrnsecond_col: col2 rnrnthird_col: col3 rnrnfourth_col: col4 rnrnfith_col: col5rn'
splitter = "rnrn | rnrn| "

new_string = re.split(splitter, string)
Answered By: Mathias R. Jessen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.