Merging and sorting log files in Python

前端 未结 5 1049
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 02:58

I am completely new to python and I have a serious problem which I cannot solve.

I have a few log files with identical structure:

[timestamp] [level]         


        
5条回答
  •  再見小時候
    2021-01-02 03:25

    As for the critical sorting function:

    def sort_key(line):
        return datetime.strptime(line.split(']')[0], '[%a %b %d %H:%M:%S %Y')
    

    This should be used as the key argument to sort or sorted, not as cmp. It is faster this way.

    Oh, and you should have

    from datetime import datetime
    

    in your code to make this work.

提交回复
热议问题