Mailing List Archive

string.replace
can anyone tell me why this breaks?

Python 1.4 (Nov 4 1997) [GCC 2.7.2.3]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import sys, string
>>> line = 'cat "in" the hat'
>>> line = string.replace( line, '"', '')
Traceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: replace
>>>


_
string.replace [ In reply to ]
Works for me.
Upgrade your python version.
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on w
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import sys, string
>>> line = 'cat "in" the hat'
>>> line = string.replace( line, '"', '')
>>> print line
cat in the hat
>>>

--
--Darrell
marlon <marlon@soda.CSUA.Berkeley.EDU> wrote in message
news:Pine.BSF.4.10.9907221446520.20655-100000@soda.CSUA.Berkeley.EDU...
> can anyone tell me why this breaks?
>
> Python 1.4 (Nov 4 1997) [GCC 2.7.2.3]
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import sys, string
> >>> line = 'cat "in" the hat'
> >>> line = string.replace( line, '"', '')
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> AttributeError: replace
> >>>
>
>
> _
>
string.replace [ In reply to ]
I tried the same routine on a machine with 1.5.1 and it worked fine,
you're right about the version being the problem. Thanks for your help.
marlon

On Thu, 22 Jul 1999, Gordon McMillan wrote:

> marlon asks:
>
> > can anyone tell me why this breaks?
> >
> > Python 1.4 (Nov 4 1997) [GCC 2.7.2.3]
> ^^^
>
> > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> > >>> import sys, string
> > >>> line = 'cat "in" the hat'
> > >>> line = string.replace( line, '"', '')
> > Traceback (innermost last):
> > File "<stdin>", line 1, in ?
> > AttributeError: replace
> > >>>
>
> I believe string.replace came with 1.5.
>
> - Gordon
>

_
string.replace [ In reply to ]
marlon asks:

> can anyone tell me why this breaks?
>
> Python 1.4 (Nov 4 1997) [GCC 2.7.2.3]
^^^

> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import sys, string
> >>> line = 'cat "in" the hat'
> >>> line = string.replace( line, '"', '')
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> AttributeError: replace
> >>>

I believe string.replace came with 1.5.

- Gordon
string.replace [ In reply to ]
marlon <marlon@CSUA.Berkeley.EDU> wrote:
: I tried the same routine on a machine with 1.5.1 and it worked fine,
: you're right about the version being the problem. Thanks for your help.
: marlon

: On Thu, 22 Jul 1999, Gordon McMillan wrote:

:> marlon asks:
:>
:> > can anyone tell me why this breaks?
:> >
:> > Python 1.4 (Nov 4 1997) [GCC 2.7.2.3]
:> ^^^
:>
:> > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
:> > >>> import sys, string
:> > >>> line = 'cat "in" the hat'
:> > >>> line = string.replace( line, '"', '')
:> > Traceback (innermost last):
:> > File "<stdin>", line 1, in ?
:> > AttributeError: replace
:> > >>>
:>
:> I believe string.replace came with 1.5.
:>
:> - Gordon

You can use string.translate and string.maketrans instead.

In Python 1.5:
exclamation = 'nu'
exclamation = string.replace(exclamation, 'u', 'i')

In Python 1.4 (and earlier):
exclamation = 'nu'
knights_map = string.maketrans('u', 'i')
exclamation = string.translate(exclamation, knights_map)

-Arcege