<small id='2KpQ1'></small><noframes id='2KpQ1'>

<i id='2KpQ1'><tr id='2KpQ1'><dt id='2KpQ1'><q id='2KpQ1'><span id='2KpQ1'><b id='2KpQ1'><form id='2KpQ1'><ins id='2KpQ1'></ins><ul id='2KpQ1'></ul><sub id='2KpQ1'></sub></form><legend id='2KpQ1'></legend><bdo id='2KpQ1'><pre id='2KpQ1'><center id='2KpQ1'></center></pre></bdo></b><th id='2KpQ1'></th></span></q></dt></tr></i><div id='2KpQ1'><tfoot id='2KpQ1'></tfoot><dl id='2KpQ1'><fieldset id='2KpQ1'></fieldset></dl></div>
    1. <tfoot id='2KpQ1'></tfoot>
        <bdo id='2KpQ1'></bdo><ul id='2KpQ1'></ul>

        <legend id='2KpQ1'><style id='2KpQ1'><dir id='2KpQ1'><q id='2KpQ1'></q></dir></style></legend>

      1. 在 Python 中合并和排序日志文件

        时间:2023-09-14
        <legend id='kpTaT'><style id='kpTaT'><dir id='kpTaT'><q id='kpTaT'></q></dir></style></legend>
            <tbody id='kpTaT'></tbody>

            1. <tfoot id='kpTaT'></tfoot>

              <small id='kpTaT'></small><noframes id='kpTaT'>

              • <bdo id='kpTaT'></bdo><ul id='kpTaT'></ul>

                  <i id='kpTaT'><tr id='kpTaT'><dt id='kpTaT'><q id='kpTaT'><span id='kpTaT'><b id='kpTaT'><form id='kpTaT'><ins id='kpTaT'></ins><ul id='kpTaT'></ul><sub id='kpTaT'></sub></form><legend id='kpTaT'></legend><bdo id='kpTaT'><pre id='kpTaT'><center id='kpTaT'></center></pre></bdo></b><th id='kpTaT'></th></span></q></dt></tr></i><div id='kpTaT'><tfoot id='kpTaT'></tfoot><dl id='kpTaT'><fieldset id='kpTaT'></fieldset></dl></div>
                  本文介绍了在 Python 中合并和排序日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我对 python 完全陌生,但我遇到了一个无法解决的严重问题.

                  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] [source] message
                  

                  例如:

                  [Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1] error message
                  

                  我需要用纯 Python 编写一个程序,它将这些日志文件合并到一个文件中,然后按时间戳对合并后的文件进行排序.在此操作之后,我希望将此结果(合并文件的内容)打印到 STDOUT(控制台).

                  I need to write a program in pure Python which should merge these log files into one file and then sort the merged file by timestamp. After this operation I wish to print this result (the contents of the merged file) to STDOUT (console).

                  我不明白该怎么做,希望得到帮助.这可能吗?

                  I don't understand how to do this would like help. Is this possible?

                  推荐答案

                  你可以这样做

                  import fileinput
                  import re
                  from time import strptime
                  
                  f_names = ['1.log', '2.log'] # names of log files
                  lines = list(fileinput.input(f_names))
                  t_fmt = '%a %b %d %H:%M:%S %Y' # format of time stamps
                  t_pat = re.compile(r'[(.+?)]') # pattern to extract timestamp
                  for l in sorted(lines, key=lambda l: strptime(t_pat.search(l).group(1), t_fmt)):
                      print l,
                  

                  这篇关于在 Python 中合并和排序日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python:用微秒将字符串转换为时间戳 下一篇:Python - 定位最近的时间戳

                  相关文章

                  最新文章

                    <bdo id='ReEKY'></bdo><ul id='ReEKY'></ul>
                    <i id='ReEKY'><tr id='ReEKY'><dt id='ReEKY'><q id='ReEKY'><span id='ReEKY'><b id='ReEKY'><form id='ReEKY'><ins id='ReEKY'></ins><ul id='ReEKY'></ul><sub id='ReEKY'></sub></form><legend id='ReEKY'></legend><bdo id='ReEKY'><pre id='ReEKY'><center id='ReEKY'></center></pre></bdo></b><th id='ReEKY'></th></span></q></dt></tr></i><div id='ReEKY'><tfoot id='ReEKY'></tfoot><dl id='ReEKY'><fieldset id='ReEKY'></fieldset></dl></div>

                    <legend id='ReEKY'><style id='ReEKY'><dir id='ReEKY'><q id='ReEKY'></q></dir></style></legend>
                    1. <small id='ReEKY'></small><noframes id='ReEKY'>

                      <tfoot id='ReEKY'></tfoot>