csv文件中特定列的总和

时间:2014-04-25 04:54:47

标签: python parsing python-2.7 csv dictionary

有一个csv文件,比如A.csv,有内容:

Place,Hotel,Food,Fare

Norway,Regal,NonVeg,5000    
Poland,Jenny,Italiano,6000    
Norway,Suzane,Vegeterian,4000   
Norway,Regal,NonVeg,5000

我必须通过在命令提示符中传递参数来解析此csv并获取输出。

示例1:

mycode.py Place

所需的输出是:

Place,Fare    
Norway,14000  
Poland,6000

示例2:

mycode.py Place Hotel

所需的输出是:

Place,Hotel,Fare
Norway,Regal,10000  
Poland,Jenny,6000  
Norway,Suzane,4000

从上面的例子中可以清楚地看出,无论你传递什么作为参数,它都会为你提供常见的Fare标题的总和。

下面是我的代码,我可以传递参数并获得输出,但我被困在Fare的总和中。任何人都可以帮助我。

import sys
import csv
import collections

d = collections.defaultdict(list)

Data = []
Result = []
Final = []
Argvs = []
argv_len = len(sys.argv)
index = 0
input = ''

file = open('A.csv', 'rb')
try:
    reader = csv.reader(file)
    for row in reader:
        Data.append(row)

    for x in range(1, argv_len):
        Argvs.append(sys.argv[x])
    Argvs.append('Fare')

    for input in Argvs:
        for y in range(0, len(Data[0])):
            if(input == Data[0][y]):
                for z in range(1, len(Data)):
                    Result.append(Data[z][y])                   
                break       
        Final.append(Result)
        Result = []

    New = []
    NewFinal = []
    for x in range(0, len(Final[0])):
        for y in range(0, len(Final)):
            New.append(Final[y][x])
        NewFinal.append(New)
        New = []
    out = {}
    for a in NewFinal:
        out.setdefault(a[0],[]).append(int(a[-1]))
    with open("output.csv", "wb") as csv_file:
        writer = csv.writer(csv_file,  dialect='excel', delimiter=',')
        writer.writerow(Argvs)
        for k,v in out.iteritems():
            writer.writerow((k,sum(v)))
except Exception,e:
    print str(e)
finally:
    file.close()

我编辑代码并尝试对其进行分组。现在我能够获得Fare但不是所需输出的汇总。

所以当我路过时:

mycode.py Place Hotel

而不是:

Place,Hotel,Fare  
Norway,Regal,10000  
Poland,Jenny,6000  
Norway,Suzane,4000  

我得到了:

Place,Hotel,Fare  
Norway,14000  
Poland,6000  

1 个答案:

答案 0 :(得分:1)

最后我设法得到了我想要的输出 下面我分享最终的代码。 \

import sys
import csv

Data = []
Result = []
Final = []
Argvs = []
argv_len = len(sys.argv)
index = 0
input = ''

file = open('A.csv', 'rb')
try:
    reader = csv.reader(file)
    for row in reader:
        Data.append(row)

    for x in range(1, argv_len):
        Argvs.append(sys.argv[x])
    Argvs.append('Fare')

    for input in Argvs:
        for y in range(0, len(Data[0])):
            if(input == Data[0][y]):
                for z in range(1, len(Data)):
                    Result.append(Data[z][y])                   
                break       
        Final.append(Result)
        Result = []

    New = []
    NewFinal = []
    for x in range(0, len(Final[0])):
        for y in range(0, len(Final)):
            New.append(Final[y][x])
        NewFinal.append(New)
        New = []
    out = {}
    for a in NewFinal:
        count_val = a[-1]
        del a[-1]
        key_val = ','.join(a)
        out.setdefault(key_val.strip('"'),[]).append(int(count_val))
    with open("output.csv", "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',',quotechar=' ')
        writer.writerow(Argvs)
        for k,v in out.iteritems():
            writer.writerow((k,sum(v)))
except Exception,e:
    print str(e)
finally:
    file.close()