python中的文本文件循环缓冲区

s23*_*50u 6 python file fifo circular-buffer

我需要一个python脚本为文本文件中的行实现循环缓冲区,限制为N行,如下所示:

        row 1 -> pop
        row 2
        row 3
         |
         |
push -> row N
Run Code Online (Sandbox Code Playgroud)

什么是最好的解决方案?

编辑:此脚本应创建和维护仅包含最新N行的文本文件.然后它应该弹出推入的第一行.就像一个fifo缓冲区.

Sve*_*ach 9

使用collections.deque.它支持一个maxlen参数.

d = collections.deque(maxlen=10)
for line in f:
    d.append(line)
    # ...
Run Code Online (Sandbox Code Playgroud)


fdb*_*fdb 2

试试我的食谱,抱歉意大利的用法:

\n\n
#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n#       fifo(.py)\n#       \n#       Copyright 2011 Fabio Di Bernardini <fdb@altraqua.com>\n#       \n#       This program is free software; you can redistribute it and/or modify\n#       it under the terms of the GNU General Public License as published by\n#       the Free Software Foundation; either version 2 of the License, or\n#       (at your option) any later version.\n#       \n#       This program is distributed in the hope that it will be useful,\n#       but WITHOUT ANY WARRANTY; without even the implied warranty of\n#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n#       GNU General Public License for more details.\n#       \n#       You should have received a copy of the GNU General Public License\n#       along with this program; if not, write to the Free Software\n#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,\n#       MA 02110-1301, USA.\n\ndef string_conditioned(string):\n    return string.decode(\'string_escape\').rstrip() + \'\\n\'\n\ndef pop(n, size, filename):\n    with open(filename, \'r+U\') as fd:\n        rows = fd.readlines()\n    with open(filename, \'w\') as fd:\n        n = int(n)\n        fd.writelines(rows[n:])\n        return \'\'.join(rows[:n])\n\ndef trim_fifo(row, size, filename):\n    size = int(size)\n    with open(filename, \'rU\') as fd:\n        rows = fd.readlines()\n    num_rows = len(rows)\n    if num_rows >= size:\n        n = string_conditioned(row).count(\'\\n\')\n        pop(num_rows + n - size, size, filename)\n\ndef push(row, size, filename):\n    trim_fifo(row, size, filename)\n    with open(filename, \'a\') as fd:\n        fd.write(string_conditioned(row))\n    return \'\'\n\ndef main():\n    import sys\n    try:\n        command  = sys.argv[1]\n        param    = sys.argv[2]\n        size     = sys.argv[3]\n        filename = sys.argv[4]\n        sys.stdout.write({\n        \'--push\': push,\n        \'--pop\' : pop,\n        }[command](param, size, filename))\n    except Exception, e:\n        print r"""\nUso:\n       fifo --push ROW MAX_ROWS FILE\n       fifo --pop  NUM MAX_ROWS FILE\n\nfifo implementa un buffer ad anello di righe di testo, Quando viene inserita\nuna riga che fa superare il numero massimo di righe (MAX_ROWS) elimina la riga\npi\xc3\xb9 vecchia.\n\nComandi:\n  --push    accoda la riga di testo ROW nel FILE rimuovendo le righe pi\xc3\xb9 vecchie\n            se il file supera MAX_ROWS. Usare \'\\n\' per separare righe multiple.\n  --pop     stampa le prime NUM righe e le rimuove dal FILE. MAX_ROWS viene\n            ignorato ma deve essere comunque specificato.\n\nEsempi:\n       fifo --push \'row_one \\n row_two\' 10 fifo.txt\n       fifo --pop 2 10 fifo.txt\n"""\n        print e\n\nif __name__ == \'__main__\':\n    main()\n
Run Code Online (Sandbox Code Playgroud)\n