17

How would I delete everything after a certain character of a string in python? For example I have a string containing a file path and some extra characters. How would I delete everything after .zip? I've tried rsplit and split , but neither included the .zip when deleting extra characters.

Any suggestions?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ThatGuyJay
  • 307
  • 2
  • 5
  • 13
  • possible duplicate of [How to remove all characters after a specific character in python?](http://stackoverflow.com/questions/904746/how-to-remove-all-characters-after-a-specific-character-in-python) – Mr. Bultitude Sep 15 '15 at 19:14

5 Answers5

23

Just take the first portion of the split, and add '.zip' back:

s = 'test.zip.zyz'
s = s.split('.zip', 1)[0] + '.zip'

Alternatively you could use slicing, here is a solution where you don't need to add '.zip' back to the result (the 4 comes from len('.zip')):

s = s[:s.index('.zip')+4]

Or another alternative with regular expressions:

import re
s = re.match(r'^.*?\.zip', s).group(0)
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
11

str.partition:

>>> s='abc.zip.blech'
>>> ''.join(s.partition('.zip')[0:2])
'abc.zip'
>>> s='abc.zip'
>>> ''.join(s.partition('.zip')[0:2])
'abc.zip'
>>> s='abc.py'
>>> ''.join(s.partition('.zip')[0:2])
'abc.py'
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
6

Use slices:

s = 'test.zip.xyz'
s[:s.index('.zip') + len('.zip')]
=> 'test.zip'

And it's easy to pack the above in a little helper function:

def removeAfter(string, suffix):
    return string[:string.index(suffix) + len(suffix)]

removeAfter('test.zip.xyz', '.zip')
=> 'test.zip'
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

I think it's easy to create a simple lambda function for this.

mystrip = lambda s, ss: s[:s.index(ss) + len(ss)]

Can be used like this:

mystr = "this should stay.zipand this should be removed."
mystrip(mystr, ".zip") # 'this should stay.zip'
joente
  • 858
  • 7
  • 9
  • Isn't this simply Óscar López's answer? I'm also not sure I see the point of using a `lambda` if you're going to immediately give it a name. – DSM Jul 26 '13 at 21:59
  • You're right but the answers showed up quickly and I didn't refreshed the page before posting.. F.J has also a similar answer. – joente Jul 26 '13 at 22:52
1

You can use the re module:

import re
re.sub('\.zip.*','.zip','test.zip.blah')
jh314
  • 27,144
  • 16
  • 62
  • 82