Python 3.7.1“;ValueError:无法将字符串转换为float:";

Python 3.7.1“;ValueError:无法将字符串转换为float:";,python,python-3.x,Python,Python 3.x,我尝试读取文件内容并将其保存为变量,尝试将其转换为浮点数,然后- Traceback (most recent call last): File "/Users/username/wageCalculator.py", line 26, in <module> writeTotal.write(str(float(total)+float(getBal))) TypeError: float() argument must be a string or a number,

我尝试读取文件内容并将其保存为变量,尝试将其转换为浮点数,然后-

Traceback (most recent call last):
  File "/Users/username/wageCalculator.py", line 26, in <module>
    writeTotal.write(str(float(total)+float(getBal)))
TypeError: float() argument must be a string or a number, not '_io.TextIOWrapper
我试着把它转换成一个字符串,但它给了我同样的错误

如果任何人都能找到答案,那就太好了p

编辑:我尝试过getBal和readBal,readBal给了我以下错误:

Traceback (most recent call last):
  File "/Users/Jonathan_Xu/wageCalculator.py", line 27, in <module>
    writeTotal.write(str(float(total)+float(readBal)))
ValueError: could not convert string to float:
回溯(最近一次呼叫最后一次):
文件“/Users/Jonathan_Xu/wageCalculator.py”,第27行,在
writeTotal.write(str(float(total)+float(readBal)))
ValueError:无法将字符串转换为浮点:

错误中的代码行与发布的代码不匹配:

writeTotal.write(str(float(total)+float(getBal)))
vs


您可能想使用
float(readBal)

getBal
是一个文件处理程序,而不是字符串。你的意思是
readBal
?可能是重复的(查看John的答案以获得澄清)。另外,我强烈建议使用
来打开文件:。干杯:)
writeTotal.write(str(float(total)+float(getBal)))
writeTotal.write(str(float(total)+float(readBal)))