每小时桶的最小和最大时间戳

时间:2015-12-14 07:43:46

标签: python python-2.7 python-datetime

我有一个带时间戳的文本文件。

示例:

16-07-2015 18:08:20
16-07-2015 18:08:22
16-07-2015 18:08:30
16-07-2015 18:08:40
17-07-2015 10:04:01
17-07-2015 10:14:31
17-07-2015 10:14:59
17-07-2015 12:24:11
....

现在我需要每小时的最小值和最大值,如下例所示。

示例:

16-07-2015 18:08:20 - 16-07-2015 18:08:40
17-07-2015 10:04:01 - 17-07-2015 10:14:59
17-07-2015 12:24:11 - ....

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

如果你有datetimefrom itertools import groupby def min_max_per_hour(iterable): for dayhour, grouped in groupby(iterable, lambda dt: (dt.date(), dt.hour)): minimum = next(grouped) # first object is the minimum for this hour maximum = minimum # starting value for dt in grouped: maximum = dt # last assignment is the maximum within this hour yield (minimum, maximum) 个对象,你可以按天和小时对它们进行分组,然后用itertools.groupby()找到第一个和最后一个:

datetime

这依赖于包含排序顺序中的from datetime import datetime with open(input_filename) as inf: # generator expression datetimes = (datetime.strptime(line.strip(), '%d-%m-%Y %H:%M:%S') for line in inf) for mindt, maxdt in min_max_per_hour(datetimes): print mindt, maxdt 个对象的iterable。

要生成输入iterable,请在生成器表达式或其他生成器中解析文本文件;没有必要一次性将所有内容保存在内存中:

>>> from datetime import datetime
>>> from itertools import groupby
>>> def min_max_per_hour(iterable):
...     for dayhour, grouped in groupby(iterable, lambda dt: (dt.date(), dt.hour)):
...         minimum = next(grouped)  # first object is the minimum for this hour
...         maximum = minimum  # starting value
...         for dt in grouped:
...             maximum = dt   # last assignment is the maximum within this hour
...         yield (minimum, maximum)
...
>>> textfile = '''\
... 16-07-2015 18:08:20
... 16-07-2015 18:08:22
... 16-07-2015 18:08:30
... 16-07-2015 18:08:40
... 17-07-2015 10:04:01
... 17-07-2015 10:14:31
... 17-07-2015 10:14:59
... 17-07-2015 12:24:11
... '''.splitlines()
>>> datetimes = (datetime.strptime(line.strip(), '%d-%m-%Y %H:%M:%S') for line in textfile)
>>> for mindt, maxdt in min_max_per_hour(datetimes):
...     print mindt, maxdt
...
2015-07-16 18:08:20 2015-07-16 18:08:40
2015-07-17 10:04:01 2015-07-17 10:14:59
2015-07-17 12:24:11 2015-07-17 12:24:11

演示:

public static void main(String[] args) throws Exception {
    // creates an input stream for the file to be parsed
    FileInputStream in = new FileInputStream("test.java");

    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(in);
    } finally {
        in.close();
    }

    // change the methods names and parameters
    changeMethods(cu);

    // prints the changed compilation unit
    System.out.println(cu.toString());
}

private static void changeMethods(CompilationUnit cu) {
    List<TypeDeclaration> types = cu.getTypes();
    for (TypeDeclaration type : types) {
        List<BodyDeclaration> members = type.getMembers();
        for (BodyDeclaration member : members) {
            if (member instanceof MethodDeclaration) {
                // what must i do?
            }
        }
    }
}