我有2个.csv(.tsv)工作表,我将它们加载到数组中。现在,我想通过numpy isclose函数比较这两个数组的每个单元格。
它适用于普通数字,但不适用于我的数组。
with open(filename) as csv_file:
reader = csv.reader(csv_file, delimiter='\t')
for row in reader:
point.append(row[0])
with open(filename2) as csv_file:
reader = csv.reader(csv_file, delimiter='\t')
for row in reader:
point2.append(row[0])
print(numpy.isclose(point,point2, atol=0.01))
错误:
print(numpy.isclose(point,point2, atol=0.01))
File "C:\Python27\lib\site-packages\numpy\core\numeric.py", line 2306, in isclose
xfin = isfinite(x)
TypeError: Not implemented for this type
即使我尝试直接从数组中输入数据(例如,point [3],point2 [3]),我也会出错。
答案 0 :(得分:0)
csv.reader
读取字符串-您最终得到两个字符串列表。
在阅读这些行时,您可能希望将值转换为float
(或者,如果需要更高的精度,则可以使用其他方法)。
with open(filename) as csv_file:
reader = csv.reader(csv_file, delimiter='\t')
point1 = [float(row[0]) for row in reader]
with open(filename2) as csv_file:
reader = csv.reader(csv_file, delimiter='\t')
point2 = [float(row[0]) for row in reader]
print(numpy.isclose(point1, point2, atol=0.01))