我需要将此CSV读取为浮点数组。我是python的新手并且不确定如何转换,因为当我在CSV中读到它时我说我正在使用类型" Dictreader"因此我为什么要转换。谢谢!
[我在哪里的屏幕截图] [1]
import csv
with open('NP.csv') as csvfile:
NP = csv.DictReader(csvfile)
with open('A.csv') as csvfule:
A = csv.DictReader(csvfile)
[r, c] = size(NP);
[r2, c2] = size(A);
for i in range(0, r):
for j in range(0, c):
NP(i,j) = float(NP(i,j));
for i in range(0, r2):
for j in range(0, c2):
A(i,j) = float(A(i,j));
答案 0 :(得分:0)
如果您尝试读取仅包含浮点数的CSV文件,可以按以下步骤操作:
import csv
def read_csv(filename):
with open(filename, newline='') as f_input:
return [list(map(float, row)) for row in csv.reader(f_input)]
NP = read_csv('NP.csv')
A = read_csv('A.csv')
如果NP.csv
包含:
11.11,22.22,33.33,44.44
55.55,66.66,77.77,88.88
NP
现在包含:
[[11.11, 22.22, 33.33, 44.44], [55.55, 66.66, 77.77, 88.88]]
在Python 3.x中测试