将每小时的数据保存到单独的txt文件中?

时间:2014-07-29 15:54:57

标签: python

这是我的代码:

import dateutil.parser
import os
import sys
import glob
def connection():
    os.chdir("record_output/")
    mydict = {}
    data_dict = {}
    for files in sorted(glob.glob("*.csv")):
        fo = open(files, "r")
        data = fo.readlines()
        removeHeader = data.pop(0)
        for lines in data:
            lines = lines.strip("\n")
            lines = lines.strip("\r")
            lines = lines.split(',')
            print lines
            dt = dateutil.parser.parse(lines[0])
            hour = dt.hour

当我打印线条时,它给出了以下输出:

['2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001']
['2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001']
['2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001']
['2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001']
['2014-07-18 02:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10002']
['2014-07-18 02:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10002']

我希望得到类似的信息:

文件01:00:00.txt

'2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001'
'2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001'
'2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001'
'2014-07-18 01:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10001'

文件02:00:00.txt

'2014-07-18 02:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10002'
'2014-07-18 02:00:00', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', 'UNKNOWN', '10002'

我希望保存在单独文本文件中的每小时的显着输出,其命名约定基于“小时”名称。假设可以有24小时。

任何帮助?

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效。只需跟踪当前时间。只要小时改变就打开一个新文件。

cur_hour = None
for line in lines:
    dt = dateutil.parser.parse(line[0])
    hour = dt.hour
    if hour != cur_hour:
        outFile = open("%d.00.00.txt" % hour, "w")
        cur_hour = hour
    print >> outFile, ', '.join(line)