如何对数据值求和 [英] How do I sum the data values

查看:88
本文介绍了如何对数据值求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

基本上我的程序当前读取数据文件(电子信息),将值相加,并在对值进行求和后,将所有负数更改为0,并保持正值他们是数字。该计划完美地完成了这一点这是我目前的代码:

Hello,
Basically currently my program reads the Data file (electric info), sums the values up, and after summing the values, it changes all the negative numbers to 0, and keeps the positive numbers as they are. The program does this perfectly. This is the code I currently have:

import csv
from datetime import timedelta
from collections import defaultdict

def convert(item): 
    try:
        return float(item)
    except ValueError:
        return 0

sums = defaultdict(list)

def daily():
    lista = []
    with open('Data.csv', 'r') as inp:
        reader = csv.reader(inp, delimiter = ';')
        headers = next(reader)
        for line in reader: 
            mittaus = max(0,sum([convert(i) for i in line[1:-2]])) 
            lista.append()
            #print(line[0],mittaus) ('#'only to check that it works ok)
daily()





我的问题是:如何保存数据列表,所以我可以每天添加所有值,所以应该看起来像这样



My question is: How can I save the data to lists, so I can add all the values per day, so should look something like this

1.1.2016;358006
2.1.2016;39
3.1.2016;0 ...
8.1.2016;239143



并且在将这些列入列表后(稍后保存到新的数据文件中),它应该计算累积值,如下所示:


And After had having these in a list (to save later on to a new data file), it should calculate the cumulative values, which would look like this:

1.1.2016;358006
2.1.2016;358045
3.1.2016;358045...
8.1.2016;597188 



这些应该在一个新的子程序中



小峰值数据文件背后的内容:https://pastebin.com/9HxwcixZ [它实际上除以';',而不是'',如同在pastebin中一样)

数据文件: https://files.fm/u/yuf4bbuk



我尝试过:




These should be in a new subprogram

Small peak what's behind the Data file: https://pastebin.com/9HxwcixZ [It's actually divided with ';' , not with ' ' as in the pastebin]
The data file: https://files.fm/u/yuf4bbuk

What I have tried:

def analysointi():
    lista = []
    reader = csv.reader(inp, delimiter = ';')
    for line in reader:
        lista.append(line)
    print(line[2])
analysointi()



考虑到这种方式,它应该不起作用,因为这些值应该已经在列表中,所以这种方式可以被转储。



我试图搜索结果,问人们,但我还没有解。我自己也解决不了,所以如果有人能帮助我,我会非常感激


Thinking about this way, it shouldn't work, as the values should already be in a list, so this way can be dumped.

I have tried to search for results, asked people, but I haven't got any solution. I just can't solve it myself, so I'd appreciate it so much if someone could help me

推荐答案

我不确定我是否理解这个问题。但是,一旦完成了第一组计算,就可以读取数据文件并进行累积计算。一旦你有了那些,你就可以写出新的数据。
Not sure I understand the issue. But once you have done the first set of calculations you can read the datafile and do the cumulative calculations. Once you have those you can then write out the new data.


这篇关于如何对数据值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆