如何修复numpy字符串错误

时间:2022-11-07 19:03:03

I am using numpy to create an array out of a .csv file. This file's top row is made up of text, so I copied this command from the article I was using to leave it out when using float(I have left in the whole code for context. The command is the [1:] in the last line)

我正在使用numpy从.csv文件创建一个数组。这个文件的第一行是由文本组成的,所以我在使用float时将这个命令从我正在使用的文章中复制掉(我已经在整个代码中留下了上下文。命令是最后的[1:]线)

import numpy as np
import csv
with open("atest.csv", 'r') as f:
    wines = list(csv.reader(f, delimiter=";"))
    print(wines[:3])
wines_array = np.array(wines[1:], dtype=np.float)

However, I still get this error message:

但是,我仍然收到此错误消息:

    wines_array = np.array(wines[1:], dtype=np.float)
ValueError: could not convert string to float: 'fixed acidity'

This indicates that it has not left out the top row. Does anyone have any help?

这表明它没有遗漏顶行。有没有人有任何帮助?

EDIT

This is my .csv file:

这是我的.csv文件:

"fixed acidity";"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5

The first line is all one row in my file.

第一行是我文件中的所有行。

1 个解决方案

#1


1  

It turned out that python wasn't parsing the file properly, so I switched libraries to pandas and slightly modified and used the code suggested by the user COLDSPEED in the comments. The error appears to be individual to me.

事实证明,python没有正确解析文件,因此我将库切换为pandas并进行了略微修改,并在评论中使用了用户COLDSPEED建议的代码。该错误似乎对我个人而言。

Here is the line of code I used: import pandas as pd; v = pd.read_csv('atestred.csv', error_bad_lines=False).values

这是我使用的代码行:将pandas导入为pd; v = pd.read_csv('atestred.csv',error_bad_lines = False).values

#1


1  

It turned out that python wasn't parsing the file properly, so I switched libraries to pandas and slightly modified and used the code suggested by the user COLDSPEED in the comments. The error appears to be individual to me.

事实证明,python没有正确解析文件,因此我将库切换为pandas并进行了略微修改,并在评论中使用了用户COLDSPEED建议的代码。该错误似乎对我个人而言。

Here is the line of code I used: import pandas as pd; v = pd.read_csv('atestred.csv', error_bad_lines=False).values

这是我使用的代码行:将pandas导入为pd; v = pd.read_csv('atestred.csv',error_bad_lines = False).values