【发布时间】:2021-01-19 22:27:46
【问题描述】:
我正在尝试读取 csv 文件并将数据存储到二维数组中。但是,在将数据放入二维数组之前,我想获取位于 csv 顶部的标题数据并将其存储在自己的数组中。
import csv
import numpy as np
with open ('parkinsons.data.csv','r') as csv_file:
row_count = sum(1 for row in csv_file) # fileObject is your csv.reader
readArray = np.zeros(shape=(row_count,24));
print(readArray)
with open ('parkinsons.data.csv','r') as csv_file:
lineCount = 0;
for line in csv_file:
if(lineCount == 0):
temp = np.char.split(line, sep=',')
lineCount = lineCount + 1;
print(temp) ##When I print this out I can see the entire array
print(temp[0])## When I try to print this out i get an error
csv_file.close()
这是我尝试从数组中打印出特定位置时遇到的错误(例如 temp[0]
但是,如果我只是打印 temp,那么它会显示所有数据。如果我尝试使用 np.shape,它会说数组是空的。
我对 python 和 numpy 有点陌生,所以欢迎任何帮助,我看到一些人遇到了同样的问题,但我明白他们为什么会遇到这些问题,我不明白我为什么会得到它。谢谢。
【问题讨论】:
-
你为什么使用
np.char.split而不是基本的python字符串分割?list.split(',')?