Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > string.replace doesn't removes ":"

Reply
Thread Tools

string.replace doesn't removes ":"

 
 
Joshua Robinson
Guest
Posts: n/a
 
      02-09-2013
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

 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      02-10-2013
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 groen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos ber Rdiger Thomas in dsa <hidbv3$om2$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
vduncan80@gmail.com
Guest
Posts: n/a
 
      02-12-2013
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 groen
>
> Kosmologen: Die Geheim-Vorhersage.
>
> - Karl Kaos ber Rdiger Thomas in dsa <hidbv3$om2$(E-Mail Removed)>


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
 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      02-13-2013
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)
 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      02-13-2013
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)
 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      02-13-2013
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
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      02-13-2013
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...poems/love.txt

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      02-13-2013
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.
 
Reply With Quote
 
jmfauth
Guest
Posts: n/a
 
      02-14-2013
On 13 fév, 21:24, 88888 Dihedral <(E-Mail Removed)> 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


 
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
AppendChild removes like nodes? =?Utf-8?B?YmVhbndlZWQ=?= ASP .Net 1 03-01-2005 05:39 PM
[OT] Microsoft's AntiSpyware Tool removes Internet Explorer T-Bone MCSE 11 01-19-2005 08:18 PM
removes a single page from the output cache Egon Berger ASP .Net 0 11-02-2004 02:38 PM
VS.NET designer removes runat='server' attribute in <title> =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 1 07-15-2004 03:24 AM
VS.NET removes "Runat=Server" without asking??? Ronald Colijn ASP .Net 1 11-27-2003 09:01 AM



Advertisments