无法将字符串转换为浮点数

时间:2016-11-09 20:41:08

标签: python string python-2.7 csv numbers

我有一个csv文件,其中包含以下字符串形式的值:

'838.5',
'830.090027',
'820',
'827.559998',
'822.880005'

我正在读这样的文件:

file = []
for line in open('project.csv'):
    our_data = line.split(",")

    data.append(our_data)

我试图通过以下方式将这些转换为浮点数:

data = [float(x) for x in file]

但是当我运行程序时出现此错误:

ValueError:无法将字符串转换为float:。

如何在不编辑csv文件的情况下解决此问题?

3 个答案:

答案 0 :(得分:1)

永远不止一种方法给猫皮肤,但这就是我要做的事情:

# Read the entire contents of the file into a string (`numbers`).
# This includes newline characters and single-quotes.
with open('project.csv') as infile:
    numbers = infile.read()

# Then remove the newline characters and single-quotes
# (replace each with the empty string, thus removing them)
# resulting in a string with numbers separated by commas
# e.g., `123.3,45.9,39.1`
numbers = numbers.replace("'","").replace("\n","")

# Then create a new list by splitting the string on comma
# and converting each individual item to a float
numbers = [float(num) for num in numbers.split(',')]

注意:

  • 如果文件非常大,您可能希望逐行迭代而不是读取整个文件。

  • 如果输入文件可能包含格式错误,则必须更加小心,以避免意外的异常

答案 1 :(得分:0)

原始文件包含引号和分隔线(\ n),但您只是试图摆脱断行(但尚未完成)。首先,您需要从split()的输出中提取字符串(引号和数字),然后您需要删除引号,然后使用float(...)将它们转换为浮点数:

for line in open('project.csv'):
    our_data = line.split(",")
    print our_data
    our_data = our_data[0][1:-1]
    print our_data
    print float(our_data)

会给你输出:

["'838.5'", '\n']
838.5
838.5
["'830.090027'", '\n']
830.090027
830.090027
["'820'", '\n']
820
820.0
["'827.559998'", '\n']
827.559998
827.559998
["'822.880005'"]
822.880005
822.880005

答案 2 :(得分:0)

您似乎对如何正确打开和拆分文件感到困惑。这应该工作。您的问题是您从文件中读取每一行然后尝试拆分它。您data的实际附加内容是这样的:

['838.5', ',']

然后你尝试将其转换为浮点数,这当然意味着Python会引发错误。相反,请读入整个文件,然后将其拆分。过滤掉任何不是数字的东西,然后将它们转换为浮点数:

with open('project.csv') as file:
    file = file.readlines()

file = [el[1:-1] for line in file for el in line.split(',')]
floats = [float(el) for el in file if el]

floats的价值:

[838.5, 830.090027, 820.0, 827.559998, 822.880005]