Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip string after third occurrence of character python

Tags:

python

I want to strip all characters after a third character, say - for instance.

I found this code online and it works but I'm having trouble learning how it works and wanted to ask so I can understand it fully.

 def indexList(s, item, i=0):
    """
    Return an index list of all occurrances of 'item' in string/list 's'.
    Optional start search position 'i'
    """
    i_list = []
    while True:
        try:
            i = s.index(item, i)
            i_list.append(i)
            i += 1
        except:
            break
    return i_list

def strip_chrs(s, subs):
    for i in range(indexList(s, subs)[-1], len(s)):
        if s[i+1].isalpha():
            return data[:i+1]

data = '115Z2113-3-777-55789ABC7777'
print strip_chrs(data, '-')

Here's my questions on the while True: line what's true? Also on the except: Except what? and why does is a break coded there?

Thanks in advance!

like image 296
canyon289 Avatar asked Nov 17 '11 16:11

canyon289


People also ask

How do you strip a string after a character in Python?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

How do you split a string on the second occurrence in Python?

a. split("-", 2) will split the string upto the second occurrence of - . a. split("-", 2)[:2] will give the first 2 elements in the list.

How do you find the nth occurrence of a character in a string in Python?

You can find the nth occurrence of a substring in a string by splitting at the substring with max n+1 splits. If the resulting list has a size greater than n+1, it means that the substring occurs more than n times.


3 Answers

Here is a way:

def trunc_at(s, d, n=3):
    "Returns s truncated at the n'th (3rd by default) occurrence of the delimiter, d."
    return d.join(s.split(d, n)[:n])

print trunc_at("115Z2113-3-777-55789ABC7777", "-")

How it works:

  1. The string s is split into a list at each occurrence of the delimiter d using s.split(d). We use the second argument to split to indicate the maximum number of splits to do (since there's no reason to keep splitting after the first n times). The result is a list, such as ["115Z2113", "3", "777", "55789ABC7777"]
  2. A slice of the first n items of the list is taken using [:n]. The result is another list, such as ["115Z2113", "3", "777"]
  3. The list is joined back into a string, placing the delimiter d between each item of the list,using d.join(...), resulting in, for example, "115Z2113-3-777"
like image 98
kindall Avatar answered Nov 15 '22 15:11

kindall


In a one-liner way :

data = '115Z2113-3-777-55789ABC7777'
strip_character = "-"
>>> strip_character.join(data.split(strip_character)[:3])
'115Z2113-3-777'
like image 20
Cédric Julien Avatar answered Nov 15 '22 15:11

Cédric Julien


On the "while True", True is simply the constant value True. So while True is loop-forever or until broken.

the except is using the exception that happens when s.index finds no more string after i as a way to break the loop. This is a Bad Thing.

try something like this (pseudocode):

while you still have string left:
   get index of next '-'
   if found, add 1 to count
   if count == 3:
      return s[index+1:]

the s[index+1:] returns the substring from the character following index, to the end.

like image 36
Charlie Martin Avatar answered Nov 15 '22 14:11

Charlie Martin