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

发布于 2024-11-14 13:24:11 字数 253 浏览 4 评论 0原文

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

        row 1 -> pop
        row 2
        row 3
         |
         |
push -> row N

什么是最佳解决方案?

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

I need a python script implementing a circular buffer for rows in a text file limited to N rows like this:

        row 1 -> pop
        row 2
        row 3
         |
         |
push -> row N

What's the best solution?

EDIT:
This script should create and maintain the text file which only contains the latest N lines. Then it should pop the first line pushed in. Like a fifo buffer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾忌 2024-11-21 13:24:11

使用collections.deque。它支持 maxlen 参数。

d = collections.deque(maxlen=10)
for line in f:
    d.append(line)
    # ...

Use collections.deque. It supports a maxlen parameter.

d = collections.deque(maxlen=10)
for line in f:
    d.append(line)
    # ...
做个ˇ局外人 2024-11-21 13:24:11

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       fifo(.py)
#       
#       Copyright 2011 Fabio Di Bernardini <[email protected]>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

def string_conditioned(string):
    return string.decode('string_escape').rstrip() + '\n'

def pop(n, size, filename):
    with open(filename, 'r+U') as fd:
        rows = fd.readlines()
    with open(filename, 'w') as fd:
        n = int(n)
        fd.writelines(rows[n:])
        return ''.join(rows[:n])

def trim_fifo(row, size, filename):
    size = int(size)
    with open(filename, 'rU') as fd:
        rows = fd.readlines()
    num_rows = len(rows)
    if num_rows >= size:
        n = string_conditioned(row).count('\n')
        pop(num_rows + n - size, size, filename)

def push(row, size, filename):
    trim_fifo(row, size, filename)
    with open(filename, 'a') as fd:
        fd.write(string_conditioned(row))
    return ''

def main():
    import sys
    try:
        command  = sys.argv[1]
        param    = sys.argv[2]
        size     = sys.argv[3]
        filename = sys.argv[4]
        sys.stdout.write({
        '--push': push,
        '--pop' : pop,
        }[command](param, size, filename))
    except Exception, e:
        print r"""
Uso:
       fifo --push ROW MAX_ROWS FILE
       fifo --pop  NUM MAX_ROWS FILE

fifo implementa un buffer ad anello di righe di testo, Quando viene inserita
una riga che fa superare il numero massimo di righe (MAX_ROWS) elimina la riga
più vecchia.

Comandi:
  --push    accoda la riga di testo ROW nel FILE rimuovendo le righe più vecchie
            se il file supera MAX_ROWS. Usare '\n' per separare righe multiple.
  --pop     stampa le prime NUM righe e le rimuove dal FILE. MAX_ROWS viene
            ignorato ma deve essere comunque specificato.

Esempi:
       fifo --push 'row_one \n row_two' 10 fifo.txt
       fifo --pop 2 10 fifo.txt
"""
        print e

if __name__ == '__main__':
    main()

Try my recipe and sorry for italian usage:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       fifo(.py)
#       
#       Copyright 2011 Fabio Di Bernardini <[email protected]>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

def string_conditioned(string):
    return string.decode('string_escape').rstrip() + '\n'

def pop(n, size, filename):
    with open(filename, 'r+U') as fd:
        rows = fd.readlines()
    with open(filename, 'w') as fd:
        n = int(n)
        fd.writelines(rows[n:])
        return ''.join(rows[:n])

def trim_fifo(row, size, filename):
    size = int(size)
    with open(filename, 'rU') as fd:
        rows = fd.readlines()
    num_rows = len(rows)
    if num_rows >= size:
        n = string_conditioned(row).count('\n')
        pop(num_rows + n - size, size, filename)

def push(row, size, filename):
    trim_fifo(row, size, filename)
    with open(filename, 'a') as fd:
        fd.write(string_conditioned(row))
    return ''

def main():
    import sys
    try:
        command  = sys.argv[1]
        param    = sys.argv[2]
        size     = sys.argv[3]
        filename = sys.argv[4]
        sys.stdout.write({
        '--push': push,
        '--pop' : pop,
        }[command](param, size, filename))
    except Exception, e:
        print r"""
Uso:
       fifo --push ROW MAX_ROWS FILE
       fifo --pop  NUM MAX_ROWS FILE

fifo implementa un buffer ad anello di righe di testo, Quando viene inserita
una riga che fa superare il numero massimo di righe (MAX_ROWS) elimina la riga
più vecchia.

Comandi:
  --push    accoda la riga di testo ROW nel FILE rimuovendo le righe più vecchie
            se il file supera MAX_ROWS. Usare '\n' per separare righe multiple.
  --pop     stampa le prime NUM righe e le rimuove dal FILE. MAX_ROWS viene
            ignorato ma deve essere comunque specificato.

Esempi:
       fifo --push 'row_one \n row_two' 10 fifo.txt
       fifo --pop 2 10 fifo.txt
"""
        print e

if __name__ == '__main__':
    main()
伪装你 2024-11-21 13:24:11
import collections

def keep_last_n_and_return_first_of_last_n(filename, n):
    with open(filename, "r") as inp:
         lines= collections.deque(inp, maxlen=n)
    with open(filename, "w") as out:
         out.writelines(lines)
    return lines[0]
import collections

def keep_last_n_and_return_first_of_last_n(filename, n):
    with open(filename, "r") as inp:
         lines= collections.deque(inp, maxlen=n)
    with open(filename, "w") as out:
         out.writelines(lines)
    return lines[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文