Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 - Fatal编程技术网

使用python排列文本文档中的数据

使用python排列文本文档中的数据,python,python-2.7,Python,Python 2.7,我有一个名为data.txt的文本文件,其中包含以下信息 03/05/2016 11:00 50 03/05/2016 11:11 10 03/05/2016 11:20 30 03/05/2016 11:33 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:12 25 03/05/2016

我有一个名为
data.txt
的文本文件,其中包含以下信息

03/05/2016 11:00  50
03/05/2016 11:11  10
03/05/2016 11:20  30
03/05/2016 11:33  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:12  25
03/05/2016 12:30  59
03/05/2016 12:44  25
03/05/2016 12:54  29
03/05/2016 12:60  25
我希望输出是这样的。此数据应存储在另一个名为
reslt.txt的文件中

03/05/2016 11:00 - 12:00 290
03/05/2016 12:00 - 13:00 257
reslt.txt
文件中的第三列是
data.txt
文件中第三列值的总和。我正在使用python 2.7

我怎样才能做到这一点

(更新) 我尝试过的事情

from collections import Counter
import re

with open('data.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('reslt.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()))
但是我没有得到任何输出

(更新) 附加输入文件

07/05/2016 19:27  0.00169
07/05/2016 19:28  0.0034
07/05/2016 19:29  0.0051
07/05/2016 19:30  0.00679
07/05/2016 19:31  0.0085
07/05/2016 19:32  0.01021
07/05/2016 19:33  0.0119
07/05/2016 19:34  0.01358
07/05/2016 19:35  0.01527
07/05/2016 19:36  0.01696
07/05/2016 19:37  0.01863
07/05/2016 19:38  0.02031
07/05/2016 19:39  0.02199
07/05/2016 19:40  0.02367
07/05/2016 19:41  0.02535
07/05/2016 19:42  0.02704
07/05/2016 19:43  0.02871
07/05/2016 19:44  0.03039
07/05/2016 19:45  0.03207
07/05/2016 19:46  0.03376
07/05/2016 19:47  0.03544
07/05/2016 19:48  0.03712
07/05/2016 19:49  0.0388
07/05/2016 19:50  0.04049
07/05/2016 19:51  0.04217
07/05/2016 19:52  0.04386
07/05/2016 19:53  0.04555
07/05/2016 19:54  0.04723
07/05/2016 19:55  0.0489
07/05/2016 19:56  0.05057
07/05/2016 19:57  0.05225
07/05/2016 19:59  0.05392
07/05/2016 20:00  0.05561
07/05/2016 20:01  0.05729
07/05/2016 20:02  0.05897
07/05/2016 20:03  0.06067
07/05/2016 20:04  0.06234
07/05/2016 20:05  0.06403
07/05/2016 20:06  0.06571
08/05/2016 20:07  0.06739
08/05/2016 20:08  0.06908
09/05/2016 20:09  0.06994
09/05/2016 20:10  0.06994
10/05/2016 20:11  0.06994
10/05/2016 20:14  0.01614
11/05/2016 19:24  0.00197
11/05/2016 19:26  0.01746

如果它对您有任何帮助,下面是我调试运行的最简洁的形式。您的原始代码对我来说很好,但这可能有助于诊断您的问题

from collections import Counter
import re

with open('data.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())

print res

with open('reslt.txt', 'w') as f:
# Write to console
    for k, v in sorted(res.iteritems()):
        print k, '||', v
        print k, '--', int(k.split()[-1]) + 1, '--', v

# Write to file
    f.writelines('{0}:00 - {1}:00 {2}\n'.format(k, int(k.split()[-1]) + 1, v)
                 for k, v in sorted(res.iteritems()))
reslt.txt:

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

不要简单地降低利率,我不明白第三列是从哪里来的?你怎么找到290号和257号?增加了什么数字?举个例子,比如(50+50+50+50=200)从11:00到12:00,即50+10+30+40+40+50+70=290@Keatinge我就是这样总结的up@Keatinge请帮我解答sir07/05/2016 19:46 0.03376 07/05/2016 19:47 0.03544 07/05/2016 19:48 0.03712 07/05/2016 19:49 0.0388 07/05/2016 19:50 0.040449 07/05/2016 19:51 0.04217 07/05/2016 19:52 0.04386 07/05/2016 19:53 0.04555 07/05/2016 19:54 0.0472307/05/2016 19:55 0.0489 07/05/2016 19:56 0.05057 07/05/2016 19:57 0.05225 07/05/2016 19:59 0.05392 07/05/2016 20:00 0.05561 07/05/2016 20:01 0.05729 07/05/2016 20:02 0.05897 07/05/2016 20:03 0.06067 07/05/2016 20:04 0.06234 07/05/2016 20:05 0.06403 07/05/2016 20:06 0.06571