Jeff
Jeff

Reputation: 509

Why am I getting "AttributeError: 'module' object has no attribute 'replace'" on string.replace()

The line causing the error is

totalR = totalR + (float(string.replace(contri[0][5],",","")) + float(string.replace(contri[0][6],",","")))

contri[0][5] and [6] are strings that contain numbers formatted as 1,000.00. I'm removing the commas before I cast the strings as floats in order to add them to totalR, which is a float. (Created as totalR = 0.0) I also tried using Decimal, but the error happened there too. I did "import string". The program fails with error:

File "mine.py", line 43, in fillDonorData
totalR = totalR + (float(string.replace(contri[0][5],",","")) + float(string.replace(contri[0][6],",","")))
AttributeError: 'module' object has no attribute 'replace'

Upvotes: 11

Views: 29057

Answers (3)

alanjds
alanjds

Reputation: 4130

It is now on str.replace on Python 3.

Looks like the same thing renamed, have the same signature and a docstring with the same meaning.

Upvotes: 5

Humoyun Ahmad
Humoyun Ahmad

Reputation: 3081

If you made changes to your module, just exit python shell and enter again and import your module again

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599778

Methods in the string module have been deprecated for years. You should call replace directly on your string, or contri[6].

Upvotes: 13

Related Questions