嘿家伙,所以我有一个文本文件,我试图读取并接收每个数字字符串 并将其转换为浮点数。但每次我尝试它时,都会说“cannont convert string to float”。这是为什么发生?谢谢!如何将字符串转换为浮动当从文本文件中读取
try:
input_file = open("Dic9812.TFITF.encoded.txt","r")
output_fileDec = open("Dic9812.TFITF.decoded.txt","w")
output_fileLog = open("Dic9812.TFITF.log.txt","w")
except IOError:
print("File not found!")
coefficientInt = input("Enter a coefficient: ")
coefficientFl = float(coefficientInt)
constInt = input("Enter a constant: ")
constFl = float(constInt)
try:
for line in input_file:
for numstr in line.split(","):
numFl = float(numstr)
print(numFl)
except Exception as e:
print(e)
文件看起来是这样的:
135.0,201.0,301.0
152.0,253.0,36.0,52.0
53.0,25.0,369.0,25.0
它结束了印刷的数字,但在最后它说: 不能把字符串转换为float:
首先,您能不能告诉你试图解析该文件的例子吗? –
其次,告诉我们'cannont convert string to float'发生在哪一行。 – 2014-02-21 21:40:10
你确定*文件中有'etc'吗?请发布一些*真实*行。 – 2014-02-21 21:45:14