On Jul 13, 6:49 pm, News123 wrote:
> I wondered about a potentially nicer way of removing a prefix of a
> string if it exists.
>
Here is an iterator solution:
from itertools import izip
def trim_prefix(prefix, s):
i1,i2 = iter(prefix),iter(s)
if all(c1==c2 for c1,c2 in izip(i1,i2)):
On 07/13/2010 09:22 PM, Shashwat Anand wrote:
You could write:
rsl = f[len(prefix):] if f.startswith(prefix) else f
Or you can just do split and join, "".join(f.split(prefix, 1)) will do.
This suggestion breaks if the prefix occurs within the string
rather than at the beginning:
f
On Wed, Jul 14, 2010 at 5:42 AM, MRAB wrote:
> News123 wrote:
>
>> I wondered about a potentially nicer way of removing a prefix of a
>> string if it exists.
>>
>>
>> Here what I tried so far:
>>
>>
>> def rm1(prefix,txt):
>>if txt.startswith(prefix):
>>return txt[len(prefix):]
>>
News123 wrote:
I wondered about a potentially nicer way of removing a prefix of a
string if it exists.
Here what I tried so far:
def rm1(prefix,txt):
if txt.startswith(prefix):
return txt[len(prefix):]
return txt
for f in [ 'file:///home/me/data.txt' , '/home/me/data.txt' ]:
Hi, for rm1 I think something like this could work:
def rm1(prefix,txt):
regular_expresion = re.compile(r''+prefix+'(.*)')
return re.match(regular_expresion,txt).group(1)
On Tue, Jul 13, 2010 at 8:49 PM, News123 wrote:
> I wondered about a potentially nicer way of removing a prefix of a
>
I wondered about a potentially nicer way of removing a prefix of a
string if it exists.
Here what I tried so far:
def rm1(prefix,txt):
if txt.startswith(prefix):
return txt[len(prefix):]
return txt
for f in [ 'file:///home/me/data.txt' , '/home/me/data.txt' ]:
# method 1 i