为什么 Python 显示“ValueError:无法将字符串转换为浮点数"? [英] Why is Python showing 'ValueError: could not convert string to float'?

查看:79
本文介绍了为什么 Python 显示“ValueError:无法将字符串转换为浮点数"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数字的 CSV 文件,我试图将其转换为浮点数.

I have a CSV containing numbers which I am trying to convert to floats.

filename = "filename.csv"
enclosed_folder = "path/to/Folder"
full_path = os.path.join(enclosed_folder,filename)

with open(full_path) as input_data:
    temp = input_data.readlines()
    n = len(temp) #int(temp.pop(0))
    matrix = [x.split(" ") for x in temp]
    for i in range(n):
        for j in range(n):
            matrix[i][j] = float(matrix[i][j])
    input_data.close()

当我在任何文本编辑器中打开文件时,它不会在每一行的末尾显示 \n.

When I open the file in any text editor, it does not show the \n at the end of each row.

但是运行 python 代码显示'ValueError: could not convert string to float',因为'\n'出现在每行的末尾.

But running the python code shows the `ValueError: could not convert string to float' because of '\n' being present at the end of each row.

Traceback (most recent call last):
  File "hierarchical-clustering.py", line 37, in <module>
    matrix[i][j] = float(matrix[i][j])
ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429\n'

那么,我该如何解决这个错误?

So, how do I fix this error?

我使用了 strip() 以及 rstrip() 作为删除空格的一些答案中的建议,但仍然错误不会消失:

I used strip() as well as rstrip() as suggested in some of the answers to remove whitespaces, but still the error does not go away:

Traceback (most recent call last):
  File "hierarchical-clustering.py", line 37, in <module>
    matrix[i][j] = float(matrix[i][j].rstrip())
ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429'

推荐答案

错误是由于您的行解析.您使用空格而不是逗号分隔,根据您的屏幕截图应该会发生这种情况.关键是看返回的错误.它试图将整行从字符串转换为浮点数.

The error is due to your line parsing. You are separating on spaces, not commas, which is what should happen according to your screenshot. The key is looking at the error returned. It is trying to convert the entire line from a string into a float.

变化:

matrix = [x.split(" ") for x in temp]

致:

matrix = [x.split(",") for x in temp]

这篇关于为什么 Python 显示“ValueError:无法将字符串转换为浮点数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆