有效地检索csv文件中的特定列

时间:2017-10-02 21:31:25

标签: python csv

我正在尝试在csv文件中为每列检索100个特定列100次。这就是我一直在做的事情。我知道它效率低下,但如果我只知道如何将它们追加100次,它似乎会起作用。

data_file_path = '2001-1.csv'
counter = 0

col0 = []
col1 = []
col2 = []
col13 = []
col16 = []
col17 = []
col18 = []

with open(data_file_path, 'r', encoding="latin-1") as fin:
    splitted = csv.reader(fin, delimiter = ',')
    next(splitted)

    for col in splitted:
        counter += 1
        col0.append(col[0])
        if(counter == 100):
            counter = 0
        col1.append(col[1])
        if(counter == 100):
            counter = 0
        col2.append(col[2])
        if(counter == 100):
            counter = 0            
        col13.append(col[13])
        if(counter == 100):
            counter = 0
        col16.append(col[16])
        if(counter == 100):
            counter = 0
        col17.append(col[17])
        if(counter == 100):
            counter = 0 
        col18.append(col[18])
        if(counter == 100):
            counter = 0
            break

flight_data = [col0, col1, col2, col13, col16, col17, col18]

但知道如何有效地做到这一点会很棒。

1 个答案:

答案 0 :(得分:0)

col这里用词不当,因为您正在迭代行和分享列。

使用字典,并保留列的列表。然后,您可以将代码减少到一个漂亮的小嵌套循环。

idx = [0, 1, 2, 13, 16, 17, 18]
data = {k : [] for k in idx}
with open(data_file_path, 'r', encoding="latin-1") as fin:
    reader = csv.reader(fin, delimiter = ',')
    next(reader)

    for row in reader:
        for i in idx:
            data[i].extend([row[i]] * 100)