简体   繁体   English

如何解决此错误:CSV文件无法将字符串转换为浮点错误

[英]How to fix this error: Could not convert string to float error with csv file

I am using a csv file and trying to take information to make a graph. 我正在使用csv文件并尝试获取信息以制作图形。 Everything looks right until I try to plot the scatter plot and it gives me the error that it couldn't convert string to float. 一切看起来都不错,直到我尝试绘制散点图,并且出现了无法将字符串转换为浮点的错误。

   matrix=[]
     doors= []
     import csv
     with open('9car.data.csv') as csvfile: 
         M=csv.reader(csvfile, delimiter=',')

         for row in M:
            rowlist= [x for x in row]
            matrix.append(rowlist)
         for index in range(len(matrix)):
             if (index==0):
                 pass
             else:
                 doors.append((matrix[index][2]))
    import matplotlib.pyplot as plt
    from math import *
    import numpy as np
    from numpy import *

    n=1
    a=np.arange(0,len(doors),n)
    y=[doors[int(x)] for x in a ]
    plt.scatter(a,y)
    plt.show()

I don't know python but I know CSV and programming and I will bet that you have values like 1,234.05 in your CSV file, where the python code does not like the comma... You may have to pre-process your CSV values, by replacing commas with nothing (removing the commas) to get it to parse the string properly into a float. 我不了解python但我知道CSV和编程知识,我敢打赌,您的CSV文件中的值类似于1,234.05 ,而python代码不喜欢逗号...您可能需要预处理CSV值,通过不使用逗号替换逗号(删除逗号)以使其正确地将字符串解析为浮点数。

Post your exact error circumstances and I will review my answer. 发布您的确切错误情况,我将审核我的答案。 For example, in Sweden the number 1234.05 would be expressed as text as 1 234,05 - so you can see the parsing problems with multilingual data 例如,在瑞典,数字1234.05将以文本形式表示为1 234,05这样您就可以看到多语言数据的解析问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM