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


Groups > comp.lang.python > #38806

Re: string.replace doesn't removes ":"

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rantingrickjohnson@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'string.': 0.04; 'subject:skip:s 10': 0.05; 'arguments': 0.07; 'python': 0.09; '[1]:': 0.09; 'dict': 0.09; 'method:': 0.09; 'to:addr:comp.lang.python': 0.09; 'tuple': 0.09; 'bug': 0.10; 'cc:addr:python-list': 0.10; 'programmer': 0.11; '"this': 0.13; '!!!!': 0.16; "'b',": 0.16; "'this": 0.16; 'bug,': 0.16; 'colons': 0.16; 'references:': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'char': 0.17; 'handles': 0.18; 'assuming': 0.22; 'sorry,': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'handling': 0.27; 'mind.': 0.27; 'replace': 0.27; 'colon': 0.29; "skip:' 10": 0.30; 'asking': 0.32; 'print': 0.32; '"")': 0.33; 'null': 0.33; 'handle': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'wrong': 0.34; 'consistent': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'why': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'list,': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'remove': 0.61; 'truly': 0.62; 'more': 0.63; 'wanting': 0.65; 'manner': 0.74; '"");': 0.84; '2013': 0.84
X-Received by 10.49.95.68 with SMTP id di4mr1397099qeb.0.1360730649635; Tue, 12 Feb 2013 20:44:09 -0800 (PST)
Newsgroups comp.lang.python
Date Tue, 12 Feb 2013 20:44:09 -0800 (PST)
In-Reply-To <mailman.1536.1360407866.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=70.196.64.56; posting-account=h3aEwQoAAACiuqX-oR3gvCVFm8lLHoWj
References <mailman.1536.1360407866.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 70.196.64.56
MIME-Version 1.0
Subject Re: string.replace doesn't removes ":"
From Rick Johnson <rantingrickjohnson@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.1736.1360730657.2939.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360730657 news.xs4all.nl 6896 [2001:888:2000:d::a6]:41536
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38806

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web