Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python进行数据提取及其求和_Python_Python 2.7_Logic - Fatal编程技术网

用python进行数据提取及其求和

用python进行数据提取及其求和,python,python-2.7,logic,Python,Python 2.7,Logic,在一个名为 data.txt 03/05/2016 11:00 50 03/05/2016 11:10 10 03/05/2016 11:20 30 03/05/2016 11:30 40 03/05/2016 11:40 40 03/05/2016 11:50 50 03/05/2016 11:60 70 03/05/2016 12:00 25 03/05/2016 12:10 69 03/05/2016 12:20 25 03/05/2016 12:3

在一个名为

data.txt

03/05/2016 11:00  50

03/05/2016 11:10  10

03/05/2016 11:20  30

03/05/2016 11:30  40

03/05/2016 11:40  40

03/05/2016 11:50  50

03/05/2016 11:60  70

03/05/2016 12:00  25

03/05/2016 12:10  69

03/05/2016 12:20  25

03/05/2016 12:30  59

03/05/2016 12:40  25

03/05/2016 12:50  29

03/05/2016 12:60  25
我想做一些数学运算,这样我就可以得到最终的结果

03/05/2016 11:00 - 12:00 290

03/05/2016 12:00 - 13:00 257
该结果存储在另一个文本文件中,如data1.txt

这里290是11:00到12:00的数据总和,257是12:00到13:00的数据总和

我想用python 2.7编写这段代码

我怎样才能做到这一点

**UPDATED**


import time 
import datetime

while 1:
    final_sensorvalue = 0
    st_time = time.time()
    crntdatetime = 0.0

    while ((time.time() - st_time) < 600.0):
        sensorvalue = 10 # read sensor value
        final_sensorvalue = final_sensorvalue + sensorvalue
        time.sleep(2)




    f = open('data.txt','a')
    crntdatetime = datetime.datetime.now()
    timestamp = crntdatetime.strftime("%d/%m/%Y %H:%M")

    outstring = str(timestamp)+"  "+str(final_sensorvalue)+ "\n"
    print outstring
    f.write(outstring)
    f.close()
    time.sleep(2)
**已更新**
导入时间
导入日期时间
而1:
最终值=0
st_time=time.time()
crntdatetime=0.0
而((time.time()-st_time)<600.0):
传感器值=10#读取传感器值
最终传感器值=最终传感器值+传感器值
时间。睡眠(2)
f=打开('data.txt','a')
crntdatetime=datetime.datetime.now()
timestamp=crntdatetime.strftime(“%d/%m/%Y%H:%m”)
outstring=str(时间戳)+“”+str(最终值)+“\n”
打印输出
f、 写入(输出)
f、 关闭()
时间。睡眠(2)

您可以这样尝试:

fo = open("data.txt","r")
lines = fo.readlines()
#print lines
d={}

for i in range(0,len(lines),2):
    l = lines[i].split()
    if int(l[1].split(":")[0]) != 23:
        time = l[1].split(":")[0] + ":00-" + str(int(l[1].split(":")[0])+1) +":00"
    else:
        time = l[1].split(":")[0] + ":00-0:00"
    #key = l[0]+"_"+l[1].split(":")[0]
    key = l[0]+"_"+time
    if key in d:
        d[key] = int(d[key]) + int(l[2])
    else:
        d[key] = int(l[2])
print d


>>> 
{'03/05/2016_11:00-12:00': 290, '03/05/2016_12:00-13:00': 257}

您可以将行转换为键为日期和小时(
'03/05/2016 11'
)且值为数字的对象,其值为
int
。然后,您可以将所有
计数器
对象添加到一起,对项目进行排序并将其写入文件:

from collections import Counter
import re

with open('test.txt') as f:
    res = sum((Counter({x.group(1): int(x.group(2))})
               for x in (re.search('(.*?):.*\s(\d+)', line) for line in f) if x),
              Counter())

with open('output.txt', 'w') as f:
    f.writelines('{0}:00 - {1}:00 {2}\n'.format(k, int(k.split()[-1]) + 1, v)
                 for k, v in sorted(res.iteritems()))
output.txt的内容

03/05/2016 11:00 - 12:00 290
03/05/2016 12:00 - 13:00 257

你能展示一下到目前为止你写的代码吗?@salomonderossi更新了