Python。 ValueError 无法将字符串转换为浮点数:
Python. ValueError could not convert string to float:
我正在尝试生成文本文件中指定列中数字的平均值。我收到一条错误消息,指出 python 无法将字符串转换为浮点数,但我看不出我可以向它传递无效字符串的位置。
def avg_col(f, col, delim=None, nhr=0):
"""
file, int, str, int -> float
Produces average of data stored in column col of file f
Requires: file has nhr header rows of data; data is separated by delim
>>> test_file = StringIO('0.0, 3.5, 2.0, 5.8, 2.1')
>>> avg_col(test_file, 2, ',', 0)
2.0
>>> test_file = StringIO('0.0, 3.5, 2.0, 5.8, 2.1')
>>> avg_col(test_file, 3, ',', 0)
5.8
"""
total = 0
count = 0
skip_rows(f, nhr)
for line in f:
if line.strip() != '':
data = line.split(delim)
col_data = data[col]
total = sum_data(col_data) + total
count = len(col_data) + count
return total / count
def sum_data(lod):
'''
(listof str) -> Real
Consume a list of string-data in a file and produce the sum
>>> sum_data(['0'])
0.0
>>> sum_data(['1.5'])
1.5
'''
data_sum = 0
for number in lod:
data_sum = data_sum + float(number)
return data_sum
您正在将一个字符串传递给sum_lod()
:
data = line.split(delim)
col_data = data[col]
total = sum_data(col_data) + total
data
是一个字符串列表,data[col]
是一个元素。
另一方面,sum_data()
期望 iterable:
def sum_data(lod):
# ...
for number in lod:
遍历一个数字然后得到单独的字符:
>>> for element in '5.8':
... print element
...
5
.
8
尝试转换此类字符串的每个元素很容易导致您尝试将 非 数字的字符转换为浮点数:
>>> float('.')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: .
要么传入一个 list 字符串:
total += sum_data([col_data])
count += 1
或简单地在您拥有的一个元素上使用 float()
:
total += float(col_data)
count += 1
我正在尝试生成文本文件中指定列中数字的平均值。我收到一条错误消息,指出 python 无法将字符串转换为浮点数,但我看不出我可以向它传递无效字符串的位置。
def avg_col(f, col, delim=None, nhr=0):
"""
file, int, str, int -> float
Produces average of data stored in column col of file f
Requires: file has nhr header rows of data; data is separated by delim
>>> test_file = StringIO('0.0, 3.5, 2.0, 5.8, 2.1')
>>> avg_col(test_file, 2, ',', 0)
2.0
>>> test_file = StringIO('0.0, 3.5, 2.0, 5.8, 2.1')
>>> avg_col(test_file, 3, ',', 0)
5.8
"""
total = 0
count = 0
skip_rows(f, nhr)
for line in f:
if line.strip() != '':
data = line.split(delim)
col_data = data[col]
total = sum_data(col_data) + total
count = len(col_data) + count
return total / count
def sum_data(lod):
'''
(listof str) -> Real
Consume a list of string-data in a file and produce the sum
>>> sum_data(['0'])
0.0
>>> sum_data(['1.5'])
1.5
'''
data_sum = 0
for number in lod:
data_sum = data_sum + float(number)
return data_sum
您正在将一个字符串传递给sum_lod()
:
data = line.split(delim)
col_data = data[col]
total = sum_data(col_data) + total
data
是一个字符串列表,data[col]
是一个元素。
sum_data()
期望 iterable:
def sum_data(lod):
# ...
for number in lod:
遍历一个数字然后得到单独的字符:
>>> for element in '5.8':
... print element
...
5
.
8
尝试转换此类字符串的每个元素很容易导致您尝试将 非 数字的字符转换为浮点数:
>>> float('.')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: .
要么传入一个 list 字符串:
total += sum_data([col_data])
count += 1
或简单地在您拥有的一个元素上使用 float()
:
total += float(col_data)
count += 1