在这个foro的帮助下,我可以创建我的Optimizator仪表板。现在,我需要做一些改变。这是代码:
import csv
import datetime
with open("prueba.csv", 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
mydict = {}
for row in your_list[1:]:
date = datetime.datetime.strptime(row[0],'%d/%m/%Y')
name = row[1]
mydict[(date,name)] = row[2:]
def convert(n):
n = n.replace(",",".").replace("%","")
try:
return float(n)
except ValueError:
return 0e0
for (day, name) in mydict:
previous_day = day - datetime.timedelta(days=1)
if (previous_day,name) in mydict:
print name, datetime.datetime.strftime(day,"%d/%m/%Y")
day2_values = mydict[(day, name)]
day1_values = mydict[(previous_day, name)]
comparer = zip(day2_values, day1_values)
for n,value in enumerate(comparer):
print "item[%d]:" % (n+2,),
if convert(value[1]) < convert(value[0]):
print value[1], "smaller than", value[0], "Incremento"
else:
print value[1], "bigger than", value[0], "Descenso"
print
>>>
Martin 18/12/2017
item[2]: 312341 smaller than 349805 Incremento
item[3]: 45368 smaller than 46818 Incremento
item[4]: 14.53% bigger than 13.38% Bajada
item[5]: 39.35 bigger than 32.98 Bajada
item[6]: 0.87 bigger than 0.70 Bajada
Jose 11 03/12/2017
item[2]: 140580 smaller than 161540 Incremento
item[3]: 4943 bigger than 4663 Bajada
item[4]: 3.52% bigger than 2.89% Bajada
item[5]: 2.04 bigger than 1.95 Bajada
item[6]: 0.41 smaller than 0.42 Incremento
Jorge cl 17/12/2017
item[2]: 156736 smaller than 164272 Incremento
item[3]: 39295 bigger than 36921 Bajada
item[4]: 25.07% bigger than 22.48% Bajada
item[5]: 19.74 bigger than 19.61 Bajada
item[6]: 0.50 smaller than 0.53 Incremento
我需要更改真实姓名的回复items[2],items[3],items[4],items[5] and items[6]
:[['"Fecha"', '"Cliente"', '"Subastas"', '"Impresiones_exchange"', '"Fill_rate"', '"Importe_a_pagar_a_medio"', '"ECPM_medio"']
另外,我需要通过Cliente名称以csv文件顺序保存返回。有可能??
答案 0 :(得分:1)
真实姓名在your_list[0]
。所以改变这一行
print "item[%d]:" % (n+2,),
到此:
print your_list[0][n+2] + ":",
要将输出保存为csv
,而不是(或同时)打印出数据,您需要将其保存,以便将其传递给csv.writer
。创建一个名为result
的列表,对于要在输出中看到的每一行,执行
row = [name, datetime.datetime.strftime(day,"%d/%m/%Y"), mylist[0][n+2],value[1], value[0]]
result.append(row)
完成result
的构建后,请按名称对其进行排序:
result.sort()
使用csv.writer()
从此列表中生成csv
文件,为writerow()
中的每一行调用编写器对象的result
方法。