Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会得到;属性错误:';模块';对象没有属性';更换'&引用;在string.replace()上_Python_String_Floating Point - Fatal编程技术网

Python 为什么我会得到;属性错误:';模块';对象没有属性';更换'&引用;在string.replace()上

Python 为什么我会得到;属性错误:';模块';对象没有属性';更换'&引用;在string.replace()上,python,string,floating-point,Python,String,Floating Point,导致错误的行是 totalR = totalR + (float(string.replace(contri[0][5],",","")) + float(string.replace(contri[0][6],",",""))) contri[0][5]和[6]是包含格式为1000.00的数字的字符串。在将字符串转换为浮点数之前,我先删除逗号,以便将它们添加到totalR,totalR是一个浮点数。(创建为totalR=0.0)我也尝试使用Decimal,但错误也发生在那里。我做了“导入字符

导致错误的行是

totalR = totalR + (float(string.replace(contri[0][5],",","")) + float(string.replace(contri[0][6],",","")))
contri[0][5]和[6]是包含格式为1000.00的数字的字符串。在将字符串转换为浮点数之前,我先删除逗号,以便将它们添加到totalR,totalR是一个浮点数。(创建为totalR=0.0)我也尝试使用Decimal,但错误也发生在那里。我做了“导入字符串”。程序失败,出现错误:

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'

string
模块中的方法已被弃用多年。您应该直接在字符串上调用
replace
,或者
contri[6]

如果您对模块进行了更改,只需退出python shell并再次输入并再次导入模块即可

它现在在python 3上启用


看起来相同的东西被重命名了,具有相同的签名和具有相同含义的docstring。

我在代码中使用了“10000”而不是“contri[0][5]”和“contri[0][6]”,一切正常。我使用的是Python 2.7。如果您在Python3上尝试这个,您应该阅读我假设您在Python3.x上?如果是这样的话,你应该在你的问题中加入这个标签。此外,您应该粘贴整个回溯,而不仅仅是错误本身。在2.x中,尽管已弃用,但回溯仍然有效。在3.x中,它们中的大多数(包括
replace
)不再存在。但是,在
string
中仍有大量未弃用的函数、常量和类
string
是一个内置模块。但这是正确的,如果进行了更改,则必须重新加载。