我有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])
任何输入赞赏!
在此先感谢。