首页 > 解决方案 > 确定 csv 表中每一列的大小

问题描述

我正在尝试从 csv 文件中读取一对列,并想知道每一对中每一列的大小。

我已经尝试了附加的代码。它会引发以下错误:(1)字符串无法转换为浮点数,只要它看到空白单元格(2)IndexError,如果要读取的第一对比其余的小。基本上, len(table) 给出了表中的最大行数。或者我在这里错过了什么。

  x1        y1         x2       y2
0.0031  0.01527   -0.41973  0.01237
0.30568 0.08478   -0.07486  0.16463
0.4986  0.26355    0.0097   0.44943
0.61884 0.5102     0.30568  0.83218
0.75758 0.90136   0.62544   0.98121
1.02977 0.96214    


# Save above data to file temp.csv (strip-off the first row) and the  try to 
# read by following code

infile = open("temp.csv", 'r')
table = []
for row in csv.reader(infile):
  table.append(row)
infile.close() # better leave that file alone.
for r in range (0, len(table)):
    for c in range(0, len(table[r])):
        table[r][c] = float(table[r][c])

float() 将给出错误:ValueError: could not convert string to float for the second set of data。

当 csv 表中的所有对大小不同时,我想知道每个单独对的大小。

标签: pythoncsv

解决方案


推荐阅读