简体   繁体   English

数据CSV仪表板python

[英]Data CSV Dashboard python

With help from this foro I can create my Optimizator dashboard.在这个论坛的帮助下,我可以创建我的优化器仪表板。 Now, I need to make some changes.现在,我需要做一些改变。 This is the code:这是代码:

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

I need to change the returns items[2],items[3],items[4],items[5] and items[6] for the real names : [['"Fecha"', '"Cliente"', '"Subastas"', '"Impresiones_exchange"', '"Fill_rate"', '"Importe_a_pagar_a_medio"', '"ECPM_medio"']我需要更改真实姓名的退货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"']

Also, I need to save the returns in a csv file order by Cliente name.此外,我需要按客户名称以 csv 文件顺序保存退货。 Is it possible??是否可以??

The real names are in your_list[0] .真实姓名在your_list[0] So change this line所以改变这一行

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

to this:对此:

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

To save the output as a csv , instead of (or as well as) printing your data out, you need to save it so that you can pass it to csv.writer .要将输出保存为csv ,而不是(或同时)打印出您的数据,您需要保存它以便您可以将其传递给csv.writer Create a list called result , and for every row that you want to see in the output, do创建一个名为result的列表,对于您想在输出中看到的每一行,请执行

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

After you have finished building result , sort it on name:完成构建result ,按名称对其进行排序:

result.sort()

Use csv.writer() to produce your csv file from this list, one call to the writer object's writerow() method for every row in result .使用csv.writer()生产的csv从这个列表文件,一个调用作家对象的writerow()方法中的每一行result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM