2016-03-15 79 views
2

我有CSV文件中像這樣:Python的排序與和CSV

日期時間,Usage1,PROJECT1
日期時間,Usage2,PROJECT1
日期時間,Usage3,Project2的
日期時間,Usage4,項目3

目標是總結每個項目的用途及有這樣的報道:

PROJECT1: Usage1 Usage2

Project2的: Usage3

項目3: Usage4

我開始用下面的Python代碼,但它不能正常工作:

#/usr/bin/python 

# obtain all Project values into new list project_tags: 

project_tags = [] 
ifile = open("file.csv","r") 
reader = csv.reader(ifile) 
headerline = ifile.next() 
for row in reader: 
    project_tags.append(str(row[2])) 
ifile.close() 

# obtain sorted and unique list and put it into a new list project_tags2 
project_tags2 = [] 
for p in list(set(project_tags)): 
    project_tags2.append(p) 


# open CSV file again and compare it with new unique list 
ifile2 = open("file.csv","r") 
reader2 = csv.reader(ifile2) 
headerline = ifile2.next() 

# Loop through both new list and a CSV file, and if they matches sum it: 

sum_per_project = sum_per_project + int(row[29]) 
for project in project_tags2: 
    for row in reader2: 
     if row[2] == project: 
      sum_per_project = sum_per_project + int(row[1]) 

任何輸入讚賞!

在此先感謝。

回答

0

下面是一個使用defaultdict的方法。

編輯: 感謝@薩利姆是提醒我with條款,而我們只需要輸出的內容

from collections import defaultdict 
import csv 

summary = defaultdict(list) 
with open(path, "r") as f: 
    rows = csv.reader(f) 
    header = rows.next() 
    for (dte, usage, proj) in rows: 
     summary[proj.strip()]+=[usage.strip()] 

# I just realized that all you needed to do was output them: 
for proj, usages in sorted(summary.iteritems()): 
    print(
     "%s: %s" % (proj, ' '.join(sorted(usages))) 
    ) 

將打印

Project1: Usage1 Usage2 
Project2: Usage3 
Project3: Usage4 
1

試試下面的代碼片段:

summary = {} 

with open("file.csv", "r") as fp: 
    for line in fp: 
     row = line.rstrip().split(',') 

     key = row[2] 
     if key in summary: 
      summary[key] += (row[1].strip(),) 
     else: 
      summary[key] = (row[1].strip(),) 

for k in summary: 
    print('{0}: {1}'.format(k, ' '.join(summary[k]))) 

根據您在csv文件中的示例數據,它將打印:

Project1: Usage1 Usage2 
Project2: Usage3 
Project3: Usage4