python如何在不使用pandas的情况下读取CSV文件指定的列

def loadData(fileName):
    x = []
    y = []
    fl = csv.reader(open(fileName,'r'))
    x = list(fl)
    y.append([row[13] for row in fl])
    return x, y 

I use this but got y is empty, my csv data is enter image description here

我想让x是所有行,而y输出[19.2,20.8]

评论
  • 嘘!安静
    嘘!安静 回复

    您需要像对y一样遍历csv文件中的行。

    x.append([row[:13] for row in fl])
    

    Please note that its recommend to use with-Syntax for file readers
    An Example for the CSV Reader from the docs:

    with open('eggs.csv', newline='') as csvfile:
    ...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    ...     for row in spamreader:
    ...         print(', '.join(row))