在CSV文件中查找各种情况

时间:2019-06-28 09:38:29

标签: python

00,0,6098
00,1,6098
00,2,6098
00,3,6098
00,4,6094
00,5,6094
01,0,8749
01,1,8749
01,2,8749
01,3,88609
01,4,88609
01,5,88609
01,6,88611
01,7,88611
01,8,88611
02,0,9006
02,1,9006
02,2,4355
02,3,9013
02,4,9013
02,5,9013
02,6,4341
02,7,4341
02,8,4341
02,9,4341
03,0,6285
03,1,6285
03,2,6285
03,3,6285
03,4,6278
03,5,6278
03,6,6278
03,7,6278
03,8,8960

我有一个csv文件,上面显示了一部分。 我想做的是,如果列0具有相同的值,它将使列2组成一个数组,并打印该数组。即-对于00,它将创建一个数组-

a = [6098,6098,6098,6098,6094,6094]

对于01,它将创建一个数组-

a = [8749,8749,88609,88609,88609,88611,88611,88611] 我不知道如何遍历此文件。

3 个答案:

答案 0 :(得分:0)

这个想法是使用一个字典,其中00、01等将是键,而value将是一个列表。因此,您需要遍历csv数据并将这些数据推入相应的键。

import csv

result = {}

with open("you csv file", "r") as csvfile:
     data = csv.reader(csvfile)
     for row in data:
         if result.has_key(row[0]):
            result[row[0]].append(row[2])
         else:
            result[row[0]] = [row[2]]
print (result)

答案 1 :(得分:0)

此解决方案假定第一列将按排序顺序显示在文件中。

def main():
    import csv
    from itertools import groupby

    with open("csv.csv") as file:
        reader = csv.reader(file)
        rows = [[row[0]] + [int(item) for item in row[1:]] for row in reader]

    groups = {}

    for key, group in groupby(rows, lambda row: row[0]):
        groups[key] = [row[2] for row in group]

    print(groups["00"])
    print(groups["01"])
    print(groups["02"])
    print(groups["03"])

    return 0

if __name__ == "__main__":
    import sys
    sys.exit(main())

输出:

[6098, 6098, 6098, 6098, 6094, 6094]
[8749, 8749, 8749, 88609, 88609, 88609, 88611, 88611, 88611]
[9006, 9006, 4355, 9013, 9013, 9013, 4341, 4341, 4341, 4341]
[6285, 6285, 6285, 6285, 6278, 6278, 6278, 6278, 8960]

答案 2 :(得分:-1)

这里

from collections import defaultdict
txt = '''00,0,6098
00,1,6098
00,2,6098
00,3,6098
00,4,6094
00,5,6094
01,0,8749
01,1,8749
01,2,8749
01,3,88609
01,4,88609
01,5,88609
01,6,88611
01,7,88611
01,8,88611
02,0,9006
02,1,9006
02,2,4355
02,3,9013
02,4,9013
02,5,9013
02,6,4341
02,7,4341
02,8,4341
02,9,4341
03,0,6285
03,1,6285
03,2,6285
03,3,6285
03,4,6278
03,5,6278
03,6,6278
03,7,6278
03,8,8960'''

data_holder = defaultdict(list)
lines = txt.split('\n')
for line in lines:
    fields = line.split(',')
    data_holder[fields[0]].append(fields[2])
for k,v in data_holder.items():
    print('{} -> {}'.format(k,v))

输出

02 -> ['9006', '9006', '4355', '9013', '9013', '9013', '4341', '4341', '4341', '4341']
03 -> ['6285', '6285', '6285', '6285', '6278', '6278', '6278', '6278', '8960']
00 -> ['6098', '6098', '6098', '6098', '6094', '6094']
01 -> ['8749', '8749', '8749', '88609', '88609', '88609', '88611', '88611', '88611']