2017-12-28 2 views
-1

이 도움을 받아 Optimizator 대시 보드를 만들 수 있습니다. 이제 약간의 변경을해야합니다.데이터 CSV 대시 보드 Python

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 파일의 순서로 수익을 저장해야합니다. 그것은 가능한가 ??

답변

1

실명은 your_list[0]입니다. 그래서이 선이에

print "item[%d]:" % (n+2,), 

변경 : 밖으로 데이터를 인쇄하여 (물론 나)

print your_list[0][n+2] + ":", 

가 대신하는 csv로 출력을 저장하려면를, 당신이 그것을 저장할 필요가 있도록 그것을 csv.writer에 전달할 수 있습니다. result라는 목록을 만들고, 당신이 result 구축을 완료 한 후에는 출력에 표시 할 모든 행에 대한 이름 종류의 그것을,

row = [name, datetime.datetime.strftime(day,"%d/%m/%Y"), mylist[0][n+2],value[1], value[0]] 
result.append(row) 

을 수행 생산

result.sort() 

사용 csv.writer() 이 목록의 파일 csvresult에있는 모든 행에 대해 작성자 개체 writerow() 메서드를 한 번 호출합니다.

+0

안녕하세요, 내가 뭔가를 변경하여 답변을 편집했습니다. 제발, 내가 복사 한 코드를 편집하고 그것을 진짜로 만들 수 있습니까? 감사합니다 –

+0

나는 편집을 볼 수 없으므로 촬영하지 않을 수 있습니다. – BoarGules

+0

답변을 작성한 후에 전체 코드를 작성할 수 있습니까? –