Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Deleting specific characters from a string

Reply
Thread Tools

Deleting specific characters from a string

 
 
Behrang Dadsetan
Guest
Posts: n/a
 
      07-09-2003
Hi all,

I would like deleting specific characters from a string.
As an example, I would like to delete all of the '@' '&' in the string
'You are ben@orange?enter&your&code' so that it becomes
'benorange?enteryourcode'.

So far I have been doing it like:
str = 'You are ben@orange?enter&your&code'
str = ''.join([ c for c in str if c not in ('@', '&')])

but that looks so ugly.. I am hoping to see nicer examples to acheive
the above..

Thanks.
Ben.

 
Reply With Quote
 
 
 
 
Matt Shomphe
Guest
Posts: n/a
 
      07-09-2003
Maybe a new method should be added to the str class, called "remove".
It would take a list of characters and remove them from the string:


class RemoveString(str):
def __init__(self, s=None):
str.__init__(self, s)
def remove(self, chars):
s = self
for c in chars:
s = s.replace(c, '')
return(s)

if __name__ == '__main__':
r = RemoveString('abc')
e = r.remove('c')
print r, e
# prints "abc ab" -- it's not "in place" removal

M@



Behrang Dadsetan <(E-Mail Removed)> wrote in message news:<begfb3$7j6$(E-Mail Removed)>...
> Hi all,
>
> I would like deleting specific characters from a string.
> As an example, I would like to delete all of the '@' '&' in the string
> 'You are ben@orange?enter&your&code' so that it becomes
> 'benorange?enteryourcode'.
>
> So far I have been doing it like:
> str = 'You are ben@orange?enter&your&code'
> str = ''.join([ c for c in str if c not in ('@', '&')])
>
> but that looks so ugly.. I am hoping to see nicer examples to acheive
> the above..
>
> Thanks.
> Ben.

 
Reply With Quote
 
 
 
 
John Hunter
Guest
Posts: n/a
 
      07-09-2003
>>>>> "Matt" == Matt Shomphe <(E-Mail Removed)> writes:

Matt> Maybe a new method should be added to the str class, called
Matt> "remove". It would take a list of characters and remove
Matt> them from the string:

you can use string translate for this, which is shorter and faster
than using the loop.

class rstr(str):
_allchars = "".join([chr(x) for x in range(256)])
def remove(self, chars):
return self.translate(self._allchars, chars)

me = rstr('John Hunter')
print me.remove('ohn')

Also, you don't need to define a separate __init__, since you are nor
overloading the str default.

JDH

 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      07-09-2003
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Matt Shomphe) wrote:

> Maybe a new method should be added to the str class, called "remove".
> It would take a list of characters and remove them from the string:


Check out the translate function - that's what its optional
deletions argument is for.

Donn Cave, (E-Mail Removed)
 
Reply With Quote
 
Behrang Dadsetan
Guest
Posts: n/a
 
      07-09-2003
Donn Cave wrote:
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (Matt Shomphe) wrote:
>>Maybe a new method should be added to the str class, called "remove".
>>It would take a list of characters and remove them from the string:

> Check out the translate function - that's what its optional
> deletions argument is for.


>>> str = 'You are Ben@orange?enter&your&code'
>>> str.translate(string.maketrans('',''), '@&')

and
>>> str.replace('&', '').replace('@', '')

are also ugly...

The first version is completely unreadable. I guess my initial example
''.join([ c for c in str if c not in ('@', '&')]) was easier to read
than the translate (who would guess -without having to peek in the
documentation of translate- that that line deletes @ and &?!) but I am
not sure

while the second becomes acceptable. The examples you gave me use the
string module.
I think I read somewhere that the methods of the object should rather be
used than the string module. Is that right?

Thanks anyhow, I will go for the replace(something, '') method.
Ben.

 
Reply With Quote
 
=?ISO-8859-1?Q?Walter_D=F6rwald?=
Guest
Posts: n/a
 
      07-09-2003
Behrang Dadsetan wrote:

> Hi all,
>
> I would like deleting specific characters from a string.
> As an example, I would like to delete all of the '@' '&' in the string
> 'You are ben@orange?enter&your&code' so that it becomes
> 'benorange?enteryourcode'.
>
> So far I have been doing it like:
> str = 'You are ben@orange?enter&your&code'
> str = ''.join([ c for c in str if c not in ('@', '&')])
>
> but that looks so ugly.. I am hoping to see nicer examples to acheive
> the above..


What about the following:

str = 'You are ben@orange?enter&your&code'
str = filter(lambda c: c not in "@&", str)

Bye,
Walter Dörwald


 
Reply With Quote
 
Behrang Dadsetan
Guest
Posts: n/a
 
      07-09-2003
Walter Dörwald wrote:

> Behrang Dadsetan wrote:
>
>> Hi all,
>>
>> I would like deleting specific characters from a string.
>> As an example, I would like to delete all of the '@' '&' in the
>> string 'You are ben@orange?enter&your&code' so that it becomes
>> 'benorange?enteryourcode'.
>>
>> So far I have been doing it like:
>> str = 'You are ben@orange?enter&your&code'
>> str = ''.join([ c for c in str if c not in ('@', '&')])
>>
>> but that looks so ugly.. I am hoping to see nicer examples to acheive
>> the above..

>
>
> What about the following:
>
> str = 'You are ben@orange?enter&your&code'
> str = filter(lambda c: c not in "@&", str)
>
> Bye,
> Walter Dörwald


def isAcceptableChar(character):
return charachter in "@&"

str = filter(isAcceptableChar, str)

is going to finally be what I am going to use.
I not feel lambdas are so readable, unless one has serious experience in
using them and python in general. I feel it is acceptable to add a named
method that documents with its name what it is doing there.

But your example would probably have been my choice if I was more
familiar with that type of use and the potential readers of my code were
also familiar with it. Many thanks!

Ben.

 
Reply With Quote
 
=?ISO-8859-1?Q?Walter_D=F6rwald?=
Guest
Posts: n/a
 
      07-09-2003
Behrang Dadsetan wrote:

> Walter Dörwald wrote:
>
>> Behrang Dadsetan wrote:
>>
>>> Hi all,
>>>
>>> I would like deleting specific characters from a string.
>>> As an example, I would like to delete all of the '@' '&' in the
>>> string 'You are ben@orange?enter&your&code' so that it becomes
>>> 'benorange?enteryourcode'.
>>>
>>> So far I have been doing it like:
>>> str = 'You are ben@orange?enter&your&code'
>>> str = ''.join([ c for c in str if c not in ('@', '&')])
>>>
>>> but that looks so ugly.. I am hoping to see nicer examples to acheive
>>> the above..

>>
>>
>>
>> What about the following:
>>
>> str = 'You are ben@orange?enter&your&code'
>> str = filter(lambda c: c not in "@&", str)
>>
>> Bye,
>> Walter Dörwald

>
>
> def isAcceptableChar(character):
> return charachter in "@&"
>
> str = filter(isAcceptableChar, str)
>
> is going to finally be what I am going to use.
> I not feel lambdas are so readable, unless one has serious experience in
> using them and python in general. I feel it is acceptable to add a named
> method that documents with its name what it is doing there.


You're not the only one with this feeling. Compare "the eff-bot's
favourite lambda refactoring rule":

http://groups.google.de/groups?selm=...ewsb.telia.net

> [...]


Bye,
Walter Dörwald

 
Reply With Quote
 
John Hunter
Guest
Posts: n/a
 
      07-09-2003
>>>>> "Behrang" == Behrang Dadsetan <(E-Mail Removed)> writes:

Behrang> is going to finally be what I am going to use. I not
Behrang> feel lambdas are so readable, unless one has serious
Behrang> experience in using them and python in general. I feel it
Behrang> is acceptable to add a named method that documents with
Behrang> its name what it is doing there.

If you want to go the functional programing route, you can generalize
your function somewhat using a callable class:

class remove_char:
def __init__(self,remove):
self.remove = dict([ (c,1) for c in remove])

def __call__(self,c):
return not self.remove.has_key(c)

print filter(remove_char('on'), 'John Hunter')

Cheers,
Jh Huter

 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-09-2003
On Wed, 09 Jul 2003 23:36:03 +0200, Behrang Dadsetan <(E-Mail Removed)> wrote:

>Walter Dörwald wrote:
>
>> Behrang Dadsetan wrote:
>>
>>> Hi all,
>>>
>>> I would like deleting specific characters from a string.
>>> As an example, I would like to delete all of the '@' '&' in the
>>> string 'You are ben@orange?enter&your&code' so that it becomes
>>> 'benorange?enteryourcode'.
>>>
>>> So far I have been doing it like:
>>> str = 'You are ben@orange?enter&your&code'
>>> str = ''.join([ c for c in str if c not in ('@', '&')])
>>>
>>> but that looks so ugly.. I am hoping to see nicer examples to acheive
>>> the above..

>>
>>
>> What about the following:
>>
>> str = 'You are ben@orange?enter&your&code'
>> str = filter(lambda c: c not in "@&", str)

Aaack! I cringe seeing builtin str name rebound like that ;-/

>>
>> Bye,
>> Walter Dörwald

>
>def isAcceptableChar(character):
> return charachter in "@&"

return character not in "@&"
>
>str = filter(isAcceptableChar, str)
>
>is going to finally be what I am going to use.

That's not going to be anywhere near as fast as Donn's translate version.

>I not feel lambdas are so readable, unless one has serious experience in
>using them and python in general. I feel it is acceptable to add a named
>method that documents with its name what it is doing there.
>
>But your example would probably have been my choice if I was more
>familiar with that type of use and the potential readers of my code were
>also familiar with it. Many thanks!
>

IMO, if you are going to define a function like isAcceptableChar, only to use it
with filter, why not write a function to do the whole job, and whose invocation
reads well, while hiding Donn's fast translate version? E.g., substituting the literal
value of string.maketrans('',''):

====< removechars.py >================================================= =======
def removeChars(s, remove=''):
return s.translate(
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\ r\x0e\x0f'
'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\ x1c\x1d\x1e\x1f'
' !"#$%&\'()*+,-./'
'0123456789:;<=>?'
'@ABCDEFGHIJKLMNO'
'PQRSTUVWXYZ[\\]^_'
'`abcdefghijklmno'
'pqrstuvwxyz{|}~\x7f'
'\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\ x8c\x8d\x8e\x8f'
'\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\ x9c\x9d\x9e\x9f'
'\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\ xac\xad\xae\xaf'
'\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\ xbc\xbd\xbe\xbf'
'\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\ xcc\xcd\xce\xcf'
'\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\ xdc\xdd\xde\xdf'
'\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\ xec\xed\xee\xef'
'\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\ xfc\xfd\xfe\xff'
, remove)

if __name__ == '__main__':
import sys
args = sys.argv[1:]
fin = sys.stdin; fout=sys.stdout; remove='' # defaults
while args:
arg = args.pop(0)
if arg == '-fi': fin = file(args.pop(0))
elif arg == '-fo': fout = file(args.pop(0))
else: remove = arg
for line in fin:
fout.write(removeChars(line, remove))
================================================== ============================
Not tested beyond what you see here

[16:40] C:\pywk\ut>echo "'You are ben@orange?enter&your&code'" |python removechars.py "@&"
"'You are benorange?enteryourcode'"

[16:41] C:\pywk\ut>echo "'You are ben@orange?enter&your&code'" |python removechars.py aeiou
"'Y r bn@rng?ntr&yr&cd'"

Copying a snip above to the clipboard and filtering that with no removes and then (lower case) vowels:

[16:41] C:\pywk\ut>getclip |python removechars.py
>I not feel lambdas are so readable, unless one has serious experience in
>using them and python in general. I feel it is acceptable to add a named
>method that documents with its name what it is doing there.
>
>But your example would probably have been my choice if I was more
>familiar with that type of use and the potential readers of my code were
>also familiar with it. Many thanks!


[16:42] C:\pywk\ut>getclip |python removechars.py aeiou
>I nt fl lmbds r s rdbl, nlss n hs srs xprnc n
>sng thm nd pythn n gnrl. I fl t s ccptbl t dd nmd
>mthd tht dcmnts wth ts nm wht t s dng thr.
>
>Bt yr xmpl wld prbbly hv bn my chc f I ws mr
>fmlr wth tht typ f s nd th ptntl rdrs f my cd wr
>ls fmlr wth t. Mny thnks!



Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check first x characters match a specific string Angus C Programming 16 11-18-2010 06:28 AM
Deleting a specific sequence of characters (linebreaks?) from astring Aldric Giacomoni Ruby 0 02-18-2009 03:57 AM
How to delete specific characters from a string? Bazsl Ruby 11 10-12-2007 12:38 PM
XSL count specific characters in string Jerry XML 2 01-13-2005 05:41 PM
RE: Deleting specific characters from a string Michael Chermside Python 1 07-10-2003 03:29 PM



Advertisments