Quantcast
Channel: Split text after the second occurrence of character - Stack Overflow
Browsing all 7 articles
Browse latest View live

Answer by Brecht Machiels for Split text after the second occurrence of...

Here's a somewhat cryptic implementation avoiding the use of join():def split(string, sep, n):"""Split `string´ at the `n`th occurrence of `sep`""" pos = reduce(lambda x, _: string.index(sep, x + 1),...

View Article



Answer by Shashank Singh Yadav for Split text after the second occurrence of...

When proceeding with the dataframe and the split neededfor the entire column values, lambda function is better than regex.df['column_name'].apply(lambda x: "-".join(x.split('-',2)[:2]))

View Article

Answer by Nuno André for Split text after the second occurrence of character

A generic form to split a string into halves on the nth occurence of the separator would be:def split(strng, sep, pos): strng = strng.split(sep) return sep.join(strng[:pos]), sep.join(strng[pos:])If...

View Article

Answer by Mike Müller for Split text after the second occurrence of character

You can use str.index():def hyphen_split(s): pos = s.index('-') try: return s[:s.index('-', pos + 1)] except ValueError: return s[:pos]test:>>>...

View Article

Answer by Christian for Split text after the second occurrence of character

You could use regular expressions:import refile_label = re.search('(.*?-.*?)-', fname).group(1)

View Article


Answer by JRodDynamite for Split text after the second occurrence of character

You can do something like this:>>> a = "some-sample-filename-to-split">>> "-".join(a.split("-", 2)[:2])'some-sample'a.split("-", 2) will split the string upto the second occurrence of...

View Article

Split text after the second occurrence of character

I need to split text before the second occurrence of the '-' character. What I have now is producing inconsistent results. I've tried various combinations of rsplit and read through and tried other...

View Article
Browsing all 7 articles
Browse latest View live




Latest Images