@StackOverflow 我有一个脚本,它将两个列计数附加到csv的末尾。
import csv
import datetime
import copy
from collections import defaultdict
with open(r"TestSingleParaSR.csv") as i, open(r"TestSingleParaSRResuls.csv", "wb") as o:
rdr = csv.reader(i)
wrt = csv.writer(o)
data, currdate = defaultdict(lambda:[0, 0, 0, 0]), None
for line in rdr:
date, name = datetime.datetime.strptime(line[0], '%d/%m/%Y'), line[7]
if date != currdate or not currdate:
for v in data.itervalues():
#print v[:2]
#print v[2:]
#print "\n"
v[:2] = v[2:]
currdate = date
wrt.writerow(line + data[name][:2])
data[name][3] += 1
if line[6] == "1": data[name][2] += 1
此代码适用于csv,例如:
02/01/2005,Data,Class xpv,4,11yo+,4,1,George Smith
02/01/2005,Data,Class xpv,4,11yo+,4,2,Ted James
02/01/2005,Data,Class xpv,4,11yo+,4,3,Emma Lilly
02/01/2005,Data,Class xpv,4,11yo+,4,5,George Smith
02/01/2005,Data,Class tn2,4,10yo+,6,4,Tom Phillips
03/01/2005,Data,Class tn2,4,10yo+,6,2,Tom Phillips
03/01/2005,Data,Class tn2,4,10yo+,6,5,George Smith
03/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips
03/01/2005,Data,Class tn2,4,10yo+,6,1,Emma Lilly
03/01/2005,Data,Class tn2,4,10yo+,6,6,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,6,Ted James
04/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips
04/01/2005,Data,Class tn2,4,10yo+,6,2,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,4,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,1,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,5,Tom Phillips
05/01/2005,Data,Class 22zn,2,10yo+,5,3,Emma Lilly
05/01/2005,Data,Class 22zn,2,10yo+,5,1,Ted James
05/01/2005,Data,Class 22zn,2,10yo+,5,2,George Smith
05/01/2005,Data,Class 22zn,2,10yo+,5,4,Emma Lilly
05/01/2005,Data,Class 22zn,2,10yo+,5,5,Tom Phillips
并返回:
02/01/2005,Data,Class xpv,4,11yo+,4,1,George Smith,0,0
02/01/2005,Data,Class xpv,4,11yo+,4,2,Ted James,0,0
02/01/2005,Data,Class xpv,4,11yo+,4,3,Emma Lilly,0,0
02/01/2005,Data,Class xpv,4,11yo+,4,5,George Smith,0,0
02/01/2005,Data,Class tn2,4,10yo+,6,4,Tom Phillips,0,0
03/01/2005,Data,Class tn2,4,10yo+,6,2,Tom Phillips,0,0
03/01/2005,Data,Class tn2,4,10yo+,6,5,George Smith,1,2
03/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips,0,0
03/01/2005,Data,Class tn2,4,10yo+,6,1,Emma Lilly,0,1
03/01/2005,Data,Class tn2,4,10yo+,6,6,George Smith,1,2
04/01/2005,Data,Class tn2,4,10yo+,6,6,Ted James,0,1
04/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips,1,2
04/01/2005,Data,Class tn2,4,10yo+,6,2,George Smith,1,4
04/01/2005,Data,Class tn2,4,10yo+,6,4,George Smith,1,4
04/01/2005,Data,Class tn2,4,10yo+,6,1,George Smith,1,4
04/01/2005,Data,Class tn2,4,10yo+,6,5,Tom Phillips,0,3
05/01/2005,Data,Class 22zn,2,10yo+,5,3,Emma Lilly,1,2
05/01/2005,Data,Class 22zn,2,10yo+,5,1,Ted James,0,2
05/01/2005,Data,Class 22zn,2,10yo+,5,2,George Smith,2,7
05/01/2005,Data,Class 22zn,2,10yo+,5,4,Emma Lilly,1,2
05/01/2005,Data,Class 22zn,2,10yo+,5,5,Tom Phillips,0,5
我希望能够将三列附加到此csv的末尾,第三列是第8列除以第9列(如果第8列= 0则显然为0)
我已经尝试了解自己如何做到这一点,因此在脚本中注释掉了打印件。但我无法锻炼什么功能wrt.writerow.
任何想法如何实现我的愿望。作为一个例子,csv的最后3行将显示为:
05/01/2005,Data,Class 22zn,2,10yo+,5,2,George Smith,2,7,0.28572814
05/01/2005,Data,Class 22zn,2,10yo+,5,4,Emma Lilly,1,2,0.5
05/01/2005,Data,Class 22zn,2,10yo+,5,5,Tom Phillips,0,5,0
答案 0 :(得分:6)
以下是对脚本的修改,添加了几行来计算商。由于您已经使用了最后两列的值,因此我只取了这些值,将它们分开,并将其作为writerow()
保存的列表中的另一个元素附加:
#!/usr/bin/env python
import csv
import datetime
import copy
from collections import defaultdict
with open(r"testin.csv") as i, open(r"TestSingleParaSRResuls.csv", "wb") as o:
rdr = csv.reader(i)
wrt = csv.writer(o)
data, currdate = defaultdict(lambda:[0, 0, 0, 0]), None
for line in rdr:
date, name = datetime.datetime.strptime(line[0], '%d/%m/%Y'), line[7]
if date != currdate or not currdate:
for v in data.itervalues():
#print v[:2]
#print v[2:]
#print "\n"
v[:2] = v[2:]
currdate = date
top,bottom = data[name][0:2]
try:
quotient = float(top)/bottom
except ZeroDivisionError:
quotient = 0
wrt.writerow(line + data[name][:2]+ [quotient])
data[name][3] += 1
if line[6] == "1": data[name][2] += 1