【发布时间】:2018-10-15 06:42:07
【问题描述】:
这是问题 I previously posted 的(不同)延续。 我以前的 csv 文件格式错误,@The Puternerd 建议我在将二维数组写入文件之前先将其展平。
这就是我所拥有的:
output = open(CSVFilepath,"w")
csvwriter=csv.writer(output, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for imagePath in glob.glob(MaskImagePath):
...
#myDescriptor is the 2d np array with varying rows and fixed columns (60)
myrow=[imageID,myDescriptor.shape[0],myDescriptor.flatten()]
这是我的 csv 文件现在的样子:
000000216739.jpg,224,[0. 0. 0. ... 0. 1. 2.]
000000001268.jpg,173,[0. 0. 0. ... 0. 1. 0.]
000000166259.jpg,195,[0. 0. 0. ... 0. 0. 2.]
000000368900.jpg,226,[0. 0. 0. ... 1. 1. 1.]
但是当我试图检索第三个项目时:
with open(CSVFilepath,'r') as fin:
reader = csv.reader(fin,delimiter=',')
for row in reader:
print(row[2])
print(type(row[2]))
print(np.array(list(row[2])))
它返回:
[0. 0. 0. ... 1. 3. 2.]
<class 'str'>
['[' '0' '.' ' ' '0' '.' ' ' '0' '.' ' ' '.' '.' '.' ' ' '1' '.' ' ' '3' '.' ' ' '2' '.' ']']
这是否意味着我没有正确保存这些值?任何建议将不胜感激!!!
**********更新**************
阅读下面的@Navneeth 说明,现在我有这个代码:
with open(CSVFilepath,'r') as fin:
reader = csv.reader(fin,delimiter=',')
for row in reader:
print(row[2])
print(type(row[2]))
a = row[2].replace("\n","")
print(a)
print(np.fromstring(a[1:-1], dtype=float, sep=" "))
但它会打印:
[0. 0. 0. ... 1. 2. 0.]
<class 'str'>
[0. 0. 0. ... 1. 2. 0.]
[0. 0. 0.]
[0. 0. 0. ... 1. 3. 2.]
<class 'str'>
[0. 0. 0. ... 1. 3. 2.]
[0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
<class 'str'>
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0.]
【问题讨论】: