Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38504 > unrolled thread

string.replace doesn't removes ":"

Started byJoshua Robinson <shooki.robinson@gmail.com>
First post2013-02-09 06:04 -0500
Last post2013-02-12 20:44 -0800
Articles 13 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  string.replace doesn't removes ":" Joshua Robinson <shooki.robinson@gmail.com> - 2013-02-09 06:04 -0500
    Re: string.replace doesn't removes ":" Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-02-10 11:36 +0100
      Re: string.replace doesn't removes ":" vduncan80@gmail.com - 2013-02-12 07:14 -0800
    Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 20:44 -0800
      Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 21:26 -0800
      Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 21:26 -0800
        Re: string.replace doesn't removes ":" jmfauth <wxjmfauth@gmail.com> - 2013-02-12 23:10 -0800
          Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:34 -0800
            Re: string.replace doesn't removes ":" Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-13 16:55 +0000
              Re: string.replace doesn't removes ":" Walter Hurry <walterhurry@lavabit.com> - 2013-02-13 18:16 +0000
            Re: string.replace doesn't removes ":" 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-13 12:24 -0800
              Re: string.replace doesn't removes ":" jmfauth <wxjmfauth@gmail.com> - 2013-02-14 00:02 -0800
    Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 20:44 -0800

#38504 — string.replace doesn't removes ":"

FromJoshua Robinson <shooki.robinson@gmail.com>
Date2013-02-09 06:04 -0500
Subjectstring.replace doesn't removes ":"
Message-ID<mailman.1536.1360407866.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hi *Monte-Pythons*,

x = "this is a simple : text: that has colon"
s = x.replace(string.punctuation, "");  OR
s = x.replace(string.punctuation, "");
print x   # 'this is a simple : text: that has colon'
# The colon is still in the text !!!!

Is this a bug or am I doing something wrong ?

Py.Version: 2.7
OS: Ubuntu 12.10 (64 bits)

Cheers,
-Joshua

[toc] | [next] | [standalone]


#38562

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2013-02-10 11:36 +0100
Message-ID<kf7t7u$jmo$1@news.albasani.net>
In reply to#38504
On 09.02.2013 12:04, Joshua Robinson wrote:
> Hi *Monte-Pythons*,
> 
> x = "this is a simple : text: that has colon"
> s = x.replace(string.punctuation, "");  OR
> s = x.replace(string.punctuation, "");
> print x   # 'this is a simple : text: that has colon'
> # The colon is still in the text !!!!
> 
> Is this a bug or am I doing something wrong ?

The latter. str.replace() only replaces complete substrings, not single
character occurences of the given pattern. That is

"foo".replace("foo", "bar") == "bar"
"foofoo".replace("foo", "bar") == "barbar"
"foofoo".replace("fo", "bar") == "barobaro"
"foofoo".replace("abcdef", "bar") == "foofoo"

Regards,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

[toc] | [prev] | [next] | [standalone]


#38755

Fromvduncan80@gmail.com
Date2013-02-12 07:14 -0800
Message-ID<d57644b0-62ed-40fa-8681-41bb3111042a@googlegroups.com>
In reply to#38562
On Sunday, February 10, 2013 4:36:53 AM UTC-6, Johannes Bauer wrote:
> On 09.02.2013 12:04, Joshua Robinson wrote:
> 
> > Hi *Monte-Pythons*,
> 
> > 
> 
> > x = "this is a simple : text: that has colon"
> 
> > s = x.replace(string.punctuation, "");  OR
> 
> > s = x.replace(string.punctuation, "");
> 
> > print x   # 'this is a simple : text: that has colon'
> 
> > # The colon is still in the text !!!!
> 
> > 
> 
> > Is this a bug or am I doing something wrong ?
> 
> 
> 
> The latter. str.replace() only replaces complete substrings, not single
> 
> character occurences of the given pattern. That is
> 
> 
> 
> "foo".replace("foo", "bar") == "bar"
> 
> "foofoo".replace("foo", "bar") == "barbar"
> 
> "foofoo".replace("fo", "bar") == "barobaro"
> 
> "foofoo".replace("abcdef", "bar") == "foofoo"
> 
> 
> 
> Regards,
> 
> Johannes
> 
> 
> 
> -- 
> 
> >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> 
> > Zumindest nicht öffentlich!
> 
> Ah, der neueste und bis heute genialste Streich unsere großen
> 
> Kosmologen: Die Geheim-Vorhersage.
> 
>  - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

Hello Joshua:

Hopefully you have worked out the issue.  Johannes is right on the money using 'replace' as shown below.

x = "this is a simple : text: that has colon
s = x.replace(":", "")
print(s)
'this is a simple  text that has colon'

Sincerely,
VDuncan

[toc] | [prev] | [next] | [standalone]


#38805

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-12 20:44 -0800
Message-ID<92122dd3-ac2e-4407-87ed-0b250ed9c8b9@googlegroups.com>
In reply to#38504
On Saturday, February 9, 2013 5:04:18 AM UTC-6, Joshua Robinson wrote:
> Hi Monte-Pythons,
> 
> x = "this is a simple : text: that has colon"
> s = x.replace(string.punctuation, "");  OR
> s = x.replace(string.punctuation, ""); 
> print x   # 'this is a simple : text: that has colon'
> 
> # The colon is still in the text !!!!
> 
> Is this a bug or am I doing something wrong ?

Not a bug, you are just expecting Python to read your mind. In actuality what you are doing is asking the method "string.replace()" to replace the 32 char string:

   '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' 

with the null string: 

   ""

...what you INTENDED to do was have Python find every occurrence the each char in the string you passed and replace them with the null string. You are assuming the replace method handles sequences in the manner you are projecting. Sorry, but string#replace does not work that way. Whether or not it should is up for debate[1].

However, you can create a loop and pass the arguments "one-by-one" into the method:

py> s = "a:b  :c: d"
py> for char in string.punctuation:
	s = s.replace(char, "")	
py> s
'ab  c d'

But this seems really wasteful if all you want is to remove colons. Are you just removing the colons or truly wanting to remove ALL punctuation from the string?

============================================================
 REFERENCES:
============================================================

[1]: Should string.replace handle list, tuple and dict arguments in addition to strings?

py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
'defg'
py> string.replace(['a', 'b', 'c'], 'abcdefgabc')
'defg'
py> string.replace({'a':'A', 'b':'2', 'c':'C'}, 'abcdefgabc')
'A2CdefgA2C'

This would be a more consistent approach to me. Handling only string arguments is woefully inadequate. Why would you have a programmer write a loop for this every time? 

@pydev&GvR
What is the justification for not processing (at the minimum) multiple arguments? Do you think strings will most often only need one modification?

[toc] | [prev] | [next] | [standalone]


#38808

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-12 21:26 -0800
Message-ID<ec8764a7-c8e7-4fc0-a95a-57a44777814e@googlegroups.com>
In reply to#38805
On Tuesday, February 12, 2013 10:44:09 PM UTC-6, Rick Johnson wrote:

> ============================================================
>  REFERENCES:
> ============================================================
> [1]: Should string.replace handle list, tuple and dict
> arguments in addition to strings?
> 
> py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
> 'defg'
> [...]

And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!

Hypothetical Examples:
    
py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
'defg'
py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
'defg'
py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
'A2CdefgA2C'

Or, an alternative to passing dict where both old and new arguments accept the sequence:

py> d = {'a':'A', 'b':'2', 'c':'C'}
py> 'abcdefgabc'.replace(d.keys(), d.values())
'A2CdefgA2C'

Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.

(hopefully there's no mistakes this time) 

[toc] | [prev] | [next] | [standalone]


#38809

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-12 21:26 -0800
Message-ID<mailman.1738.1360733201.2939.python-list@python.org>
In reply to#38805
On Tuesday, February 12, 2013 10:44:09 PM UTC-6, Rick Johnson wrote:

> ============================================================
>  REFERENCES:
> ============================================================
> [1]: Should string.replace handle list, tuple and dict
> arguments in addition to strings?
> 
> py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
> 'defg'
> [...]

And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!

Hypothetical Examples:
    
py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
'defg'
py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
'defg'
py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
'A2CdefgA2C'

Or, an alternative to passing dict where both old and new arguments accept the sequence:

py> d = {'a':'A', 'b':'2', 'c':'C'}
py> 'abcdefgabc'.replace(d.keys(), d.values())
'A2CdefgA2C'

Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.

(hopefully there's no mistakes this time) 

[toc] | [prev] | [next] | [standalone]


#38813

Fromjmfauth <wxjmfauth@gmail.com>
Date2013-02-12 23:10 -0800
Message-ID<25d2297a-6fee-4ef5-bcbd-e26a28cf6ce4@e10g2000vbv.googlegroups.com>
In reply to#38809
On 13 fév, 06:26, Rick Johnson <rantingrickjohn...@gmail.com> wrote:
> On Tuesday, February 12, 2013 10:44:09 PM UTC-6, Rick Johnson wrote:
> > ============================================================
> >  REFERENCES:
> > ============================================================
> > [1]: Should string.replace handle list, tuple and dict
> > arguments in addition to strings?
>
> > py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
> > 'defg'
> > [...]
>
> And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!
>
> Hypothetical Examples:
>
> py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
> 'defg'
> py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
> 'defg'
> py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
> 'A2CdefgA2C'
>
> Or, an alternative to passing dict where both old and new arguments accept the sequence:
>
> py> d = {'a':'A', 'b':'2', 'c':'C'}
> py> 'abcdefgabc'.replace(d.keys(), d.values())
> 'A2CdefgA2C'
>
> Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.
>
> (hopefully there's no mistakes this time)

--------

>>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
>>> 'abcdefgabc'.translate(d)
'A2CdefgA2C'
>>>
>>>
>>> def jmTranslate(s, table):
...     table = {ord(k):table[k] for k in table}
...     return s.translate(table)
...
>>> d = {'a': 'A', 'b': '2', 'c': 'C'}
>>> jmTranslate('abcdefgabc', d)
'A2CdefgA2C'
>>> d = {'a': None, 'b': None, 'c': None}
>>> jmTranslate('abcdefgabc', d)
'defg'
>>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
>>> jmTranslate('abcdefgabc', d)
'€€€€€€€€€€€€€defg€€€€€€€€€€€€€'
>>>


jmf

[toc] | [prev] | [next] | [standalone]


#38822

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-13 08:34 -0800
Message-ID<fbca1681-2814-4331-91c1-cce852ce530d@googlegroups.com>
In reply to#38813
On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
>
> >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
> >>> 'abcdefgabc'.translate(d)
> 'A2CdefgA2C'
> >>>
> >>>
> >>> def jmTranslate(s, table):
> ...     table = {ord(k):table[k] for k in table}
> ...     return s.translate(table)
> ...
> >>> d = {'a': 'A', 'b': '2', 'c': 'C'}
> >>> jmTranslate('abcdefgabc', d)
> 'A2CdefgA2C'
> >>> d = {'a': None, 'b': None, 'c': None}
> >>> jmTranslate('abcdefgabc', d)
> 'defg'
> >>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
> >>> jmTranslate('abcdefgabc', d)
> '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

[quip] I just really prefer a cryptic solution to a problem when a simplistic and consistent approach would suffice.[/quip] TO HELL WITH THE ZEN!

"Beautiful is better than ugly."
  BROKEN!

"Explicit is better than implicit."
  BROKEN!

"Simple is better than complex."
  BROKEN!

"Sparse is better than dense."
  BROKEN!

"Readability counts."
  BROKEN BROKEN BROKEN!!!!

"Special cases aren't special enough to break the rules."
  BROKEN!

"In the face of ambiguity, refuse the temptation to guess."
  BROKEN!

"There should be one-- and preferably only one --obvious way to do it."
  BROKEN BROKEN BROKEN!

"If the implementation is hard to explain, it's a bad idea."
  BROKEN!

"If the implementation is easy to explain, it may be a good idea."
  REINFORCED BY BAD EXAMPLE

[toc] | [prev] | [next] | [standalone]


#38823

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-02-13 16:55 +0000
Message-ID<mailman.1746.1360774412.2939.python-list@python.org>
In reply to#38822
On 13/02/2013 16:34, Rick Johnson wrote:
> On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
>>
>>>>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
>>>>> 'abcdefgabc'.translate(d)
>> 'A2CdefgA2C'
>>>>>
>>>>>
>>>>> def jmTranslate(s, table):
>> ...     table = {ord(k):table[k] for k in table}
>> ...     return s.translate(table)
>> ...
>>>>> d = {'a': 'A', 'b': '2', 'c': 'C'}
>>>>> jmTranslate('abcdefgabc', d)
>> 'A2CdefgA2C'
>>>>> d = {'a': None, 'b': None, 'c': None}
>>>>> jmTranslate('abcdefgabc', d)
>> 'defg'
>>>>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
>>>>> jmTranslate('abcdefgabc', d)
>> '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'
>
> [quip] I just really prefer a cryptic solution to a problem when a simplistic and consistent approach would suffice.[/quip] TO HELL WITH THE ZEN!
>
> "Beautiful is better than ugly."
>    BROKEN!
>
> "Explicit is better than implicit."
>    BROKEN!
>
> "Simple is better than complex."
>    BROKEN!
>
> "Sparse is better than dense."
>    BROKEN!
>
> "Readability counts."
>    BROKEN BROKEN BROKEN!!!!
>
> "Special cases aren't special enough to break the rules."
>    BROKEN!
>
> "In the face of ambiguity, refuse the temptation to guess."
>    BROKEN!
>
> "There should be one-- and preferably only one --obvious way to do it."
>    BROKEN BROKEN BROKEN!
>
> "If the implementation is hard to explain, it's a bad idea."
>    BROKEN!
>
> "If the implementation is easy to explain, it may be a good idea."
>    REINFORCED BY BAD EXAMPLE
>

jmf and rr in combination reminded me of this.  I hope you all get my 
drift :)

http://www.cc.gatech.edu/fac/Spencer.Rugaber/poems/love.txt

-- 
Cheers.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#38825

FromWalter Hurry <walterhurry@lavabit.com>
Date2013-02-13 18:16 +0000
Message-ID<kfgla1$cci$1@news.albasani.net>
In reply to#38823
On Wed, 13 Feb 2013 16:55:36 +0000, Mark Lawrence wrote:

> On 13/02/2013 16:34, Rick Johnson wrote:
>> On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
>>>
>>>>>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
>>>>>> 'abcdefgabc'.translate(d)
>>> 'A2CdefgA2C'
>>>>>>
>>>>>>
>>>>>> def jmTranslate(s, table):
>>> ...     table = {ord(k):table[k] for k in table}
>>> ...     return s.translate(table)
>>> ...
>>>>>> d = {'a': 'A', 'b': '2', 'c': 'C'}
>>>>>> jmTranslate('abcdefgabc', d)
>>> 'A2CdefgA2C'
>>>>>> d = {'a': None, 'b': None, 'c': None}
>>>>>> jmTranslate('abcdefgabc', d)
>>> 'defg'
>>>>>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
>>>>>> jmTranslate('abcdefgabc', d)
>>> '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'
>>
>> [quip] I just really prefer a cryptic solution to a problem when a
>> simplistic and consistent approach would suffice.[/quip] TO HELL WITH
>> THE ZEN!
>>
>> "Beautiful is better than ugly."
>>    BROKEN!
>>
>> "Explicit is better than implicit."
>>    BROKEN!
>>
>> "Simple is better than complex."
>>    BROKEN!
>>
>> "Sparse is better than dense."
>>    BROKEN!
>>
>> "Readability counts."
>>    BROKEN BROKEN BROKEN!!!!
>>
>> "Special cases aren't special enough to break the rules."
>>    BROKEN!
>>
>> "In the face of ambiguity, refuse the temptation to guess."
>>    BROKEN!
>>
>> "There should be one-- and preferably only one --obvious way to do it."
>>    BROKEN BROKEN BROKEN!
>>
>> "If the implementation is hard to explain, it's a bad idea."
>>    BROKEN!
>>
>> "If the implementation is easy to explain, it may be a good idea."
>>    REINFORCED BY BAD EXAMPLE
>>
>>
> jmf and rr in combination reminded me of this.  I hope you all get my
> drift :)
> 
> http://www.cc.gatech.edu/fac/Spencer.Rugaber/poems/love.txt

10-4, good buddy.

[toc] | [prev] | [next] | [standalone]


#38829

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-02-13 12:24 -0800
Message-ID<4ca31c17-1ec6-43dc-806f-5ef7925334b9@googlegroups.com>
In reply to#38822
Rick Johnson於 2013年2月14日星期四UTC+8上午12時34分11秒寫道:
> On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
> 
> >
> 
> > >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
> 
> > >>> 'abcdefgabc'.translate(d)
> 
> > 'A2CdefgA2C'
> 
> > >>>
> 
> > >>>
> 
> > >>> def jmTranslate(s, table):
> 
> > ...     table = {ord(k):table[k] for k in table}
> 
> > ...     return s.translate(table)
> 
> > ...
> 
> > >>> d = {'a': 'A', 'b': '2', 'c': 'C'}
> 
> > >>> jmTranslate('abcdefgabc', d)
> 
> > 'A2CdefgA2C'
> 
> > >>> d = {'a': None, 'b': None, 'c': None}
> 
> > >>> jmTranslate('abcdefgabc', d)
> 
> > 'defg'
> 
> > >>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
> 
> > >>> jmTranslate('abcdefgabc', d)
> 
> > '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'
> 
In python the variables of value types, and the variables of lists and 
dictionaries are passed to functions somewhat different.

This should be noticed by any serious programmer in python.

[toc] | [prev] | [next] | [standalone]


#38853

Fromjmfauth <wxjmfauth@gmail.com>
Date2013-02-14 00:02 -0800
Message-ID<8a53eab7-e0aa-4488-920e-2b7080949497@h9g2000vbk.googlegroups.com>
In reply to#38829
On 13 fév, 21:24, 88888 Dihedral <dihedral88...@googlemail.com> wrote:
> Rick Johnson於 2013年2月14日星期四UTC+8上午12時34分11秒寫道:
>
>
>
>
>
>
>
> > On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
>
> > > >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
>
> > > >>> 'abcdefgabc'.translate(d)
>
> > > 'A2CdefgA2C'
>
> > > >>> def jmTranslate(s, table):
>
> > > ...     table = {ord(k):table[k] for k in table}
>
> > > ...     return s.translate(table)
>
> > > ...
>
> > > >>> d = {'a': 'A', 'b': '2', 'c': 'C'}
>
> > > >>> jmTranslate('abcdefgabc', d)
>
> > > 'A2CdefgA2C'
>
> > > >>> d = {'a': None, 'b': None, 'c': None}
>
> > > >>> jmTranslate('abcdefgabc', d)
>
> > > 'defg'
>
> > > >>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
>
> > > >>> jmTranslate('abcdefgabc', d)
>
> > > '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'
>
> In python the variables of value types, and the variables of lists and
> dictionaries are passed to functions somewhat different.
>
> This should be noticed by any serious programmer in python.

---------

The purpose of my quick and dirty fct was to
show it's possible to create a text replacement
fct which is using exclusively text / strings
via a dict. (Even if in my exemple, I'm using
- and can use - None as an empty string !)


You are right.

It is also arguable, that beeing forced to have
to use a number in order to replace a character,
may not be a so good idea.

This should be noticed by any serious language designer.

More seriously.
.translate() is a very nice and underestimated method.

jmf

[toc] | [prev] | [next] | [standalone]


#38806

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-12 20:44 -0800
Message-ID<mailman.1736.1360730657.2939.python-list@python.org>
In reply to#38504
On Saturday, February 9, 2013 5:04:18 AM UTC-6, Joshua Robinson wrote:
> Hi Monte-Pythons,
> 
> x = "this is a simple : text: that has colon"
> s = x.replace(string.punctuation, "");  OR
> s = x.replace(string.punctuation, ""); 
> print x   # 'this is a simple : text: that has colon'
> 
> # The colon is still in the text !!!!
> 
> Is this a bug or am I doing something wrong ?

Not a bug, you are just expecting Python to read your mind. In actuality what you are doing is asking the method "string.replace()" to replace the 32 char string:

   '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' 

with the null string: 

   ""

...what you INTENDED to do was have Python find every occurrence the each char in the string you passed and replace them with the null string. You are assuming the replace method handles sequences in the manner you are projecting. Sorry, but string#replace does not work that way. Whether or not it should is up for debate[1].

However, you can create a loop and pass the arguments "one-by-one" into the method:

py> s = "a:b  :c: d"
py> for char in string.punctuation:
	s = s.replace(char, "")	
py> s
'ab  c d'

But this seems really wasteful if all you want is to remove colons. Are you just removing the colons or truly wanting to remove ALL punctuation from the string?

============================================================
 REFERENCES:
============================================================

[1]: Should string.replace handle list, tuple and dict arguments in addition to strings?

py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
'defg'
py> string.replace(['a', 'b', 'c'], 'abcdefgabc')
'defg'
py> string.replace({'a':'A', 'b':'2', 'c':'C'}, 'abcdefgabc')
'A2CdefgA2C'

This would be a more consistent approach to me. Handling only string arguments is woefully inadequate. Why would you have a programmer write a loop for this every time? 

@pydev&GvR
What is the justification for not processing (at the minimum) multiple arguments? Do you think strings will most often only need one modification?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web