【问题标题】:How to read a file in reverse order?如何以相反的顺序读取文件?
【发布时间】:2011-01-19 02:18:52
【问题描述】:

如何使用python以相反的顺序读取文件?我想从最后一行读取一个文件到第一行。

【问题讨论】:

  • 您的意思是“以相反的顺序阅读”还是“以相反的顺序处理行”?有区别。对于第一个,文件可能无法同时全部放入内存,因此您希望以相反的顺序处理这些行,但您无法读取整个文件并将其反转。对于第二个,您可能只是读入整个文件,并在处理它们之前反转行列表。那么它是什么?
  • 我推荐这个——没有内存问题而且速度很快:stackoverflow.com/a/260433/1212562

标签: python file reverse


【解决方案1】:

这是一种 Python 3.8+ 方法,使用两个字符串缓冲区,具有类似 grep 的子字符串匹配(或者如果传递了空子字符串,则只是简单地迭代每一行)。我希望这比将所有文件加载到内存中更有效(您可以控制缓冲区大小,这有时是可取的),例如如果您只想在文件末尾查找某些内容。 Gist here.

from __future__ import annotations

from io import StringIO, SEEK_END
from pathlib import Path
from typing import Iterator, TextIO


def grep_backwards(
    fh: TextIO,
    match_substr: str,
    line_ending: str = "\n",
    strip_eol: bool = False,
    step: int = 10,
) -> Iterator[str]:
    """
    Helper for scanning a file line by line from the end, imitating the behaviour of
    the Unix command line tools ``grep`` (when passed ``match_substr``) or ``tac`` (when
    ``match_substr`` is the empty string ``""``, i.e. matching all lines).

    Args:
      fh            : The file handle to read from
      match_substr  : Substring to match at. If given as the empty string, gives a
                      reverse line iterator rather than a reverse matching line iterator.
      line_ending   : The line ending to split lines on (default: "\n" newline)
      strip_eol     : Whether to strip (default: ``True``) or keep (``False``) line
                      endings off the end of the strings returned by the iterator.
      step          : Number of characters to load into chunk buffer (i.e. chunk size)
    """
    # Store the end of file (EOF) position as we are advancing backwards from there
    file_end_pos = fh.seek(0, SEEK_END)  # cursor has moved to EOF
    # Keep a reversed string line buffer as we are writing right-to-left
    revlinebuf = StringIO()
    # Keep a [left-to-right] string buffer as we read left-to-right, one chunk at a time
    chunk_buf = StringIO()
    # Initialise 'last chunk start' at position after the EOF (unreachable by ``read``)
    last_chunk_start = file_end_pos + 1
    line_offset = 0  # relative to SEEK_END
    has_EOF_newline = False  # may change upon finding first newline
    # In the worst case, seek all the way back to the start (position 0)
    while last_chunk_start > 0:
        # Ensure that read(size=step) will read at least 1 character
        # e.g. when step=4, last_chunk_start=3, reduce step to 3 --> chunk=[0,1,2]
        if step > last_chunk_start:
            step = last_chunk_start
        chunk_start = last_chunk_start - step
        fh.seek(chunk_start)
        # Read in the chunk for the current step (possibly after pre-existing chunks)
        chunk_buf.write(fh.read(step))
        while chunk := chunk_buf.getvalue():
            # Keep reading intra-chunk lines RTL, leaving any leftovers in revlinebuf
            lhs, EOL_match, rhs = chunk.rpartition(line_ending)
            if EOL_match:
                if line_offset == 0:
                    has_EOF_newline = rhs == ""
                # Reverse the right-hand-side of the rightmost line_ending and
                # insert it after anything already in the reversed line buffer
                if rhs:
                    # Only bother writing rhs to line buffer if there's anything in it
                    revlinebuf.write(rhs[::-1])
                # Un-reverse the line buffer --> full line after the line_ending match
                completed_line = revlinebuf.getvalue()[::-1]  # (may be empty string)
                # Clear the reversed line buffer
                revlinebuf.seek(0)
                revlinebuf.truncate()
                # `grep` if line matches (or behaves like `tac` if match_substr == "")
                if line_offset == 0:
                    if not has_EOF_newline and match_substr in completed_line:
                        # The 0'th line from the end (by definition) cannot get an EOL
                        yield completed_line
                elif match_substr in (completed_line + line_ending):
                    if not strip_eol:
                        completed_line += line_ending
                    yield completed_line
                line_offset += 1
            else:
                # If line_ending not found in chunk then add entire [remaining] chunk,
                # in reverse, onto the reversed line buffer, before chunk_buf is cleared
                revlinebuf.write(chunk_buf.getvalue()[::-1])
            # The LHS of the rightmost line_ending (if any) may contain another line
            # ending so truncate the chunk to that and re-iterate (else clear chunk_buf)
            chunk_buf.seek(len(lhs))
            chunk_buf.truncate()
        last_chunk_start = chunk_start
    if completed_line := revlinebuf.getvalue()[::-1]:
        # Iteration has reached the line at start of file, left over in the line buffer
        if line_offset == 0 and not has_EOF_newline and match_substr in completed_line:
            # The 0'th line from the end (by definition) cannot get an EOL
            yield completed_line
        elif match_substr in (
            completed_line + (line_ending if line_offset > 1 or has_EOF_newline else "")
        ):
            if line_offset == 1:
                if has_EOF_newline and not strip_eol:
                    completed_line += line_ending
            elif not strip_eol:
                completed_line += line_ending
            yield completed_line
    else:
        raise StopIteration

这里有一些测试表明它有效,3 个测试输入文件通过计数多达 100 个说“Hi 0”、“Hi 9”、“Hi 18”...:

  • ...并给数字 27 一个双换行符
  • ...并在文件末尾不添加换行符
  • ... 并在文件末尾添加 2 个换行符
# Write lines counting to 100 saying 'Hi 0', 'Hi 9', ... give number 27 a double newline
str_out = "".join([f"Hi {i}\n" if i != 27 else f"Hi {i}\n\n" for i in range(0, 100, 9)])
example_file = Path("example.txt")
no_eof_nl_file = Path("no_eof_nl.txt")  # no end of file newline
double_eof_nl_file = Path("double_eof_nl.txt")  # double end of file newline

with open(example_file, "w") as f_out:
    f_out.write(str_out)

with open(no_eof_nl_file, "w") as f_out:
    f_out.write(str_out.rstrip("\n"))

with open(double_eof_nl_file, "w") as f_out:
    f_out.write(str_out + "\n")

file_list = [example_file, no_eof_nl_file, double_eof_nl_file]
labels = [
    "EOF_NL    ",
    "NO_EOF_NL ",
    "DBL_EOF_NL",
]

print("------------------------------------------------------------")
print()
print(f"match_substr = ''")
for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        lines_rev_from_iterator = list(grep_backwards(fh=fh, match_substr=""))

    with open(each_file, "r") as fh:
        lines_rev_from_readline = list(reversed(fh.readlines()))

    print(label, f"{lines_rev_from_iterator == lines_rev_from_readline=}")
print()

for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        reverse_iterator = grep_backwards(fh=fh, match_substr="")
        first_match = next(reverse_iterator)
    print(label, f"{first_match=}")
print()

for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        all_matches = list(grep_backwards(fh=fh, match_substr=""))
    print(label, f"{all_matches=}")
print()
print()
print("------------------------------------------------------------")
print()
print(f"match_substr = 'Hi 9'")

for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        reverse_iterator = grep_backwards(fh=fh, match_substr="Hi 9")
        first_match = next(reverse_iterator)
    print(label, f"{first_match=}")
print()

for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        all_matches = list(grep_backwards(fh=fh, match_substr="Hi 9"))
    print(label, f"{all_matches=}")
print()
print("------------------------------------------------------------")
print()
print(f"match_substr = '\\n'")

for len_flag in (True, False):
    for label, each_file in zip(labels, file_list):
        with open(each_file, "r") as fh:
            lines_rev_from_iterator = list(grep_backwards(fh=fh, match_substr="\n"))
        if len_flag:
            print(label, f"{len(lines_rev_from_iterator)=}")
        else:
            print(label, f"{lines_rev_from_iterator=}")
    print()

for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        reverse_iterator = grep_backwards(fh=fh, match_substr="\n")
        first_match = next(reverse_iterator)
    print(label, f"{first_match=}")
print()

for label, each_file in zip(labels, file_list):
    with open(each_file, "r") as fh:
        all_matches = list(grep_backwards(fh=fh, match_substr="\n"))
    print(label, f"{all_matches=}")
print()
print("------------------------------------------------------------")

------------------------------------------------------------

match_substr = ''
EOF_NL     lines_rev_from_iterator == lines_rev_from_readline=True
NO_EOF_NL  lines_rev_from_iterator == lines_rev_from_readline=True
DBL_EOF_NL lines_rev_from_iterator == lines_rev_from_readline=True

EOF_NL     first_match='Hi 99\n'
NO_EOF_NL  first_match='Hi 99'
DBL_EOF_NL first_match='\n'

EOF_NL     all_matches=['Hi 99\n', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']
NO_EOF_NL  all_matches=['Hi 99', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']
DBL_EOF_NL all_matches=['\n', 'Hi 99\n', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']


------------------------------------------------------------

match_substr = 'Hi 9'
EOF_NL     first_match='Hi 99\n'
NO_EOF_NL  first_match='Hi 99'
DBL_EOF_NL first_match='Hi 99\n'

EOF_NL     all_matches=['Hi 99\n', 'Hi 90\n', 'Hi 9\n']
NO_EOF_NL  all_matches=['Hi 99', 'Hi 90\n', 'Hi 9\n']
DBL_EOF_NL all_matches=['Hi 99\n', 'Hi 90\n', 'Hi 9\n']

------------------------------------------------------------

match_substr = '\n'
EOF_NL     len(lines_rev_from_iterator)=13
NO_EOF_NL  len(lines_rev_from_iterator)=12
DBL_EOF_NL len(lines_rev_from_iterator)=14

EOF_NL     lines_rev_from_iterator=['Hi 99\n', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']
NO_EOF_NL  lines_rev_from_iterator=['Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']
DBL_EOF_NL lines_rev_from_iterator=['\n', 'Hi 99\n', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']

EOF_NL     first_match='Hi 99\n'
NO_EOF_NL  first_match='Hi 90\n'
DBL_EOF_NL first_match='\n'

EOF_NL     all_matches=['Hi 99\n', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']
NO_EOF_NL  all_matches=['Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']
DBL_EOF_NL all_matches=['\n', 'Hi 99\n', 'Hi 90\n', 'Hi 81\n', 'Hi 72\n', 'Hi 63\n', 'Hi 54\n', 'Hi 45\n', 'Hi 36\n', '\n', 'Hi 27\n', 'Hi 18\n', 'Hi 9\n', 'Hi 0\n']

------------------------------------------------------------

【讨论】:

    【解决方案2】:

    接受的答案不适用于内存无法容纳的大文件的情况(这种情况并不罕见)。

    正如其他人所指出的,@srohde answer 看起来不错,但它有下一个问题:

    • 打开文件看起来是多余的,当我们可以传递文件对象并将其留给用户决定应该以哪种编码读取时,
    • 即使我们重构以接受文件对象,它也不适用于所有编码:我们可以选择具有utf-8 编码和非 ascii 内容的文件,例如
    й
    

    通过buf_size 等于1 并且将有

        UnicodeDecodeError: 'utf8' codec can't decode byte 0xb9 in position 0: invalid start byte
    

    当然文本可能更大,但buf_size 可能会被拾取,因此会导致类似上述的混淆错误,

    • 我们无法指定自定义行分隔符,
    • 我们不能选择保留行分隔符。

    所以考虑到所有这些问题,我编写了单独的函数:

    • 一种适用于字节流的方法,
    • 第二个处理文本流并将其底层字节流委托给第一个并解码结果行。

    首先让我们定义下一个实用函数:

    ceil_division 用于进行天花板划分(与标准// 地板划分相比,更多信息可以在this thread 中找到)

    def ceil_division(left_number, right_number):
        """
        Divides given numbers with ceiling.
        """
        return -(-left_number // right_number)
    

    split 用于通过给定分隔符从右端拆分字符串并能够保留它:

    def split(string, separator, keep_separator):
        """
        Splits given string by given separator.
        """
        parts = string.split(separator)
        if keep_separator:
            *parts, last_part = parts
            parts = [part + separator for part in parts]
            if last_part:
                return parts + [last_part]
        return parts
    

    read_batch_from_end 从二进制流的右端读取批处理

    def read_batch_from_end(byte_stream, size, end_position):
        """
        Reads batch from the end of given byte stream.
        """
        if end_position > size:
            offset = end_position - size
        else:
            offset = 0
            size = end_position
        byte_stream.seek(offset)
        return byte_stream.read(size)
    

    之后我们可以定义函数来逆序读取字节流

    import functools
    import itertools
    import os
    from operator import methodcaller, sub
    
    
    def reverse_binary_stream(byte_stream, batch_size=None,
                              lines_separator=None,
                              keep_lines_separator=True):
        if lines_separator is None:
            lines_separator = (b'\r', b'\n', b'\r\n')
            lines_splitter = methodcaller(str.splitlines.__name__,
                                          keep_lines_separator)
        else:
            lines_splitter = functools.partial(split,
                                               separator=lines_separator,
                                               keep_separator=keep_lines_separator)
        stream_size = byte_stream.seek(0, os.SEEK_END)
        if batch_size is None:
            batch_size = stream_size or 1
        batches_count = ceil_division(stream_size, batch_size)
        remaining_bytes_indicator = itertools.islice(
                itertools.accumulate(itertools.chain([stream_size],
                                                     itertools.repeat(batch_size)),
                                     sub),
                batches_count)
        try:
            remaining_bytes_count = next(remaining_bytes_indicator)
        except StopIteration:
            return
    
        def read_batch(position):
            result = read_batch_from_end(byte_stream,
                                         size=batch_size,
                                         end_position=position)
            while result.startswith(lines_separator):
                try:
                    position = next(remaining_bytes_indicator)
                except StopIteration:
                    break
                result = (read_batch_from_end(byte_stream,
                                              size=batch_size,
                                              end_position=position)
                          + result)
            return result
    
        batch = read_batch(remaining_bytes_count)
        segment, *lines = lines_splitter(batch)
        yield from lines[::-1]
        for remaining_bytes_count in remaining_bytes_indicator:
            batch = read_batch(remaining_bytes_count)
            lines = lines_splitter(batch)
            if batch.endswith(lines_separator):
                yield segment
            else:
                lines[-1] += segment
            segment, *lines = lines
            yield from lines[::-1]
        yield segment
    

    最后可以定义一个反转文本文件的函数:

    import codecs
    
    
    def reverse_file(file, batch_size=None,
                     lines_separator=None,
                     keep_lines_separator=True):
        encoding = file.encoding
        if lines_separator is not None:
            lines_separator = lines_separator.encode(encoding)
        yield from map(functools.partial(codecs.decode,
                                         encoding=encoding),
                       reverse_binary_stream(
                               file.buffer,
                               batch_size=batch_size,
                               lines_separator=lines_separator,
                               keep_lines_separator=keep_lines_separator))
    

    测试

    准备工作

    我使用 fsutil command 生成了 4 个文件:

    1. empty.txt 没有内容,大小为 0MB
    2. tiny.txt 大小为 1MB
    3. small.txt 大小为 10MB
    4. large.txt 大小为 50MB

    我还重构了@srohde 解决方案以使用文件对象而不是文件路径。

    测试脚本

    from timeit import Timer
    
    repeats_count = 7
    number = 1
    create_setup = ('from collections import deque\n'
                    'from __main__ import reverse_file, reverse_readline\n'
                    'file = open("{}")').format
    srohde_solution = ('with file:\n'
                       '    deque(reverse_readline(file,\n'
                       '                           buf_size=8192),'
                       '          maxlen=0)')
    azat_ibrakov_solution = ('with file:\n'
                             '    deque(reverse_file(file,\n'
                             '                       lines_separator="\\n",\n'
                             '                       keep_lines_separator=False,\n'
                             '                       batch_size=8192), maxlen=0)')
    print('reversing empty file by "srohde"',
          min(Timer(srohde_solution,
                    create_setup('empty.txt')).repeat(repeats_count, number)))
    print('reversing empty file by "Azat Ibrakov"',
          min(Timer(azat_ibrakov_solution,
                    create_setup('empty.txt')).repeat(repeats_count, number)))
    print('reversing tiny file (1MB) by "srohde"',
          min(Timer(srohde_solution,
                    create_setup('tiny.txt')).repeat(repeats_count, number)))
    print('reversing tiny file (1MB) by "Azat Ibrakov"',
          min(Timer(azat_ibrakov_solution,
                    create_setup('tiny.txt')).repeat(repeats_count, number)))
    print('reversing small file (10MB) by "srohde"',
          min(Timer(srohde_solution,
                    create_setup('small.txt')).repeat(repeats_count, number)))
    print('reversing small file (10MB) by "Azat Ibrakov"',
          min(Timer(azat_ibrakov_solution,
                    create_setup('small.txt')).repeat(repeats_count, number)))
    print('reversing large file (50MB) by "srohde"',
          min(Timer(srohde_solution,
                    create_setup('large.txt')).repeat(repeats_count, number)))
    print('reversing large file (50MB) by "Azat Ibrakov"',
          min(Timer(azat_ibrakov_solution,
                    create_setup('large.txt')).repeat(repeats_count, number)))
    

    注意:我使用collections.deque 类来排放发电机。

    输出

    对于 Windows 10 上的 PyPy 3.5:

    reversing empty file by "srohde" 8.31e-05
    reversing empty file by "Azat Ibrakov" 0.00016090000000000028
    reversing tiny file (1MB) by "srohde" 0.160081
    reversing tiny file (1MB) by "Azat Ibrakov" 0.09594989999999998
    reversing small file (10MB) by "srohde" 8.8891863
    reversing small file (10MB) by "Azat Ibrakov" 5.323388100000001
    reversing large file (50MB) by "srohde" 186.5338368
    reversing large file (50MB) by "Azat Ibrakov" 99.07450229999998
    

    对于 Windows 10 上的 CPython 3.5:

    reversing empty file by "srohde" 3.600000000000001e-05
    reversing empty file by "Azat Ibrakov" 4.519999999999958e-05
    reversing tiny file (1MB) by "srohde" 0.01965560000000001
    reversing tiny file (1MB) by "Azat Ibrakov" 0.019207699999999994
    reversing small file (10MB) by "srohde" 3.1341862999999996
    reversing small file (10MB) by "Azat Ibrakov" 3.0872588000000007
    reversing large file (50MB) by "srohde" 82.01206720000002
    reversing large file (50MB) by "Azat Ibrakov" 82.16775059999998
    

    因此,我们可以看到它的性能与原始解决方案相似,但更通用且没有上面列出的缺点。


    广告

    我已将此添加到lz package0.3.0 版本(需要Python 3.5+),该版本具有许多经过良好测试的功能/迭代实用程序。

    可以像这样使用

    import io
    from lz.reversal import reverse
    ...
    with open('path/to/file') as file:
         for line in reverse(file, batch_size=io.DEFAULT_BUFFER_SIZE):
             print(line)
    

    它支持all standard encodings(可能除了utf-7,因为我很难定义a strategy 来生成可用它编码的字符串)。

    【讨论】:

    • from lz.iterating import reverse 现在应该是 from lz.reversal import reverse
    • 现在将这个模块代码的彻底性\流灵活性与 Berislav 的 python 版本灵活性、简单性(独立而不是一组巨大的函数)和易于安装(简单的 pip 命令)结合起来答案...但是你们做得很好,非常感谢您为我们提供了一些更直接和可移植的东西,而不是把杂乱的功能扔到我们自己的代码\模块中。无法想象这些年来有多少人不得不重新发明这个轮子。
    【解决方案3】:

    我不认为之前已经提到过,但是使用collectionsreverse 中的deque 对我有用:

    from collections import deque
    
    fs = open("test.txt","rU")
    fr = deque(fs)
    fr.reverse()  # reverse in-place, returns None
    
    for li in fr:
       print li
    
    fs.close()
    

    【讨论】:

      【解决方案4】:
      def previous_line(self, opened_file):
              opened_file.seek(0, os.SEEK_END)
              position = opened_file.tell()
              buffer = bytearray()
              while position >= 0:
                  opened_file.seek(position)
                  position -= 1
                  new_byte = opened_file.read(1)
                  if new_byte == self.NEW_LINE:
                      parsed_string = buffer.decode()
                      yield parsed_string
                      buffer = bytearray()
                  elif new_byte == self.EMPTY_BYTE:
                      continue
                  else:
                      new_byte_array = bytearray(new_byte)
                      new_byte_array.extend(buffer)
                      buffer = new_byte_array
              yield None
      

      使用:

      opened_file = open(filepath, "rb")
      iterator = self.previous_line(opened_file)
      line = next(iterator) #one step
      close(opened_file)
      

      【讨论】:

        【解决方案5】:

        with open("filename") as f:

            print(f.read()[::-1])
        

        【讨论】:

        • 这会读入整个文件吗?这对大文件安全吗?这似乎是一种非常简单和现实的方法,但不确定上述问题..我想以这种方式搜索文件(使用 re)..
        • @ikwyl6 这应该等同于list(reversed(f.read()))
        【解决方案6】:

        作为生成器编写的正确、有效的答案。

        import os
        
        def reverse_readline(filename, buf_size=8192):
            """A generator that returns the lines of a file in reverse order"""
            with open(filename) as fh:
                segment = None
                offset = 0
                fh.seek(0, os.SEEK_END)
                file_size = remaining_size = fh.tell()
                while remaining_size > 0:
                    offset = min(file_size, offset + buf_size)
                    fh.seek(file_size - offset)
                    buffer = fh.read(min(remaining_size, buf_size))
                    remaining_size -= buf_size
                    lines = buffer.split('\n')
                    # The first line of the buffer is probably not a complete line so
                    # we'll save it and append it to the last line of the next buffer
                    # we read
                    if segment is not None:
                        # If the previous chunk starts right from the beginning of line
                        # do not concat the segment to the last line of new chunk.
                        # Instead, yield the segment first 
                        if buffer[-1] != '\n':
                            lines[-1] += segment
                        else:
                            yield segment
                    segment = lines[0]
                    for index in range(len(lines) - 1, 0, -1):
                        if lines[index]:
                            yield lines[index]
                # Don't yield None if the file was empty
                if segment is not None:
                    yield segment
        

        【讨论】:

        • 这不适用于 python>=3.2 中的 text 文件,因为由于某种原因,不再支持相对于文件末尾的查找。可以通过保存fh.seek(0, os.SEEK_END) 返回的文件大小并更改fh.seek(-offset, os.SEEK_END) 来修复fh.seek(file_size - offset)
        • 进行编辑后,这在 python 3.5 中完美运行。该问题的最佳答案。
        • revert this change for python 2 其中fh.seek() 返回None
        • 请注意,这对于文本文件可能无法正常工作。以相反的顺序正确获取块仅适用于二进制文件。问题是对于多字节编码的文本文件(如utf8),seek()read()指的是不同的大小。这可能也是不支持seek() 相对于os.SEEK_END 的非零第一个参数的原因。
        • 简单:'aöaö'.encode()b'a\xc3\xb6a\xc3\xb6'。如果你把它保存到磁盘然后以文本模式读取,当你做seek(2)时,它会移动两个字节,所以seek(2); read(1)会导致错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte,但是如果你这样做seek(0); read(2); read(1),你会得到您期望的'a',即:seek() 永远不会编码,read() 是如果您以文本模式打开文件。现在如果有'aöaö' * 1000000,你的块将不会正确对齐。
        【解决方案7】:
        import sys
        f = open(sys.argv[1] , 'r')
        for line in f.readlines()[::-1]:
            print line
        

        【讨论】:

        【解决方案8】:

        逐行读取文件,然后以相反的顺序将其添加到列表中。

        下面是代码示例:

        reverse = []
        with open("file.txt", "r") as file:
            for line in file:
                line = line.strip()
                 reverse[0:0] = line
        

        【讨论】:

        【解决方案9】:

        大多数答案需要在做任何事情之前阅读整个文件。该样本从末尾读取越来越大的样本。

        我在写这个答案时只看到了 Murat Yükselen 的答案。这几乎是一样的,我认为这是一件好事。下面的示例还处理 \r 并在每一步增加其缓冲区大小。我也有一些unit tests 来支持这段代码。

        def readlines_reversed(f):
            """ Iterate over the lines in a file in reverse. The file must be
            open in 'rb' mode. Yields the lines unencoded (as bytes), including the
            newline character. Produces the same result as readlines, but reversed.
            If this is used to reverse the line in a file twice, the result is
            exactly the same.
            """
            head = b""
            f.seek(0, 2)
            t = f.tell()
            buffersize, maxbuffersize = 64, 4096
            while True:
                if t <= 0:
                    break
                # Read next block
                buffersize = min(buffersize * 2, maxbuffersize)
                tprev = t
                t = max(0, t - buffersize)
                f.seek(t)
                lines = f.read(tprev - t).splitlines(True)
                # Align to line breaks
                if not lines[-1].endswith((b"\n", b"\r")):
                    lines[-1] += head  # current tail is previous head
                elif head == b"\n" and lines[-1].endswith(b"\r"):
                    lines[-1] += head  # Keep \r\n together
                elif head:
                    lines.append(head)
                head = lines.pop(0)  # can be '\n' (ok)
                # Iterate over current block in reverse
                for line in reversed(lines):
                    yield line
            if head:
                yield head
        

        【讨论】:

          【解决方案10】:

          您需要首先以读取格式打开文件,将其保存到变量中,然后以写入格式打开第二个文件,您将在其中使用 [::-1] 切片写入或附加变量,完全反转文件。您还可以使用 readlines() 将其变为行列表,您可以对其进行操作

          def copy_and_reverse(filename, newfile):
              with open(filename) as file:
                  text = file.read()
              with open(newfile, "w") as file2:
                  file2.write(text[::-1])
          

          【讨论】:

            【解决方案11】:

            你也可以使用python模块file_read_backwards

            安装后,通过pip install file_read_backwards (v1.2.1),您可以通过以下方式以内存高效的方式向后(逐行)读取整个文件:

            #!/usr/bin/env python2.7
            
            from file_read_backwards import FileReadBackwards
            
            with FileReadBackwards("/path/to/file", encoding="utf-8") as frb:
                for l in frb:
                     print l
            

            它支持“utf-8”、“latin-1”和“ascii”编码。

            也支持 python3。更多文档可以在http://file-read-backwards.readthedocs.io/en/latest/readme.html找到。

            【讨论】:

            • 感谢您的解决方案。我喜欢(也赞成)@srohde 的上述解决方案,因为它帮助我理解了它是如何完成的,但作为一名开发人员,我更喜欢尽可能使用现有模块,所以我很高兴知道这个。
            • 这适用于像 UTF-8 这样的多字节编码。 seek/read 解决方案没有:seek() 以字节为单位,read() 以字符为单位。
            • 在我只想读取最后 500kB 左右的 42MB 文本文件上,这明显比读取/解析整个文件慢(CentOS 7 上的 python 3.6.8)。我主要是在寻找一种方法来让我的解析程序在文件增长时保持快速,因为我只需要最后 N 个匹配项。
            【解决方案12】:

            如果您担心文件大小/内存使用情况,内存映射文件并向后扫描换行符是一种解决方案:

            How to search for a string in text files?

            【讨论】:

              【解决方案13】:

              创建第二个反转文件的简单函数(仅限 Linux):

              import os
              def tac(file1, file2):
                   print(os.system('tac %s > %s' % (file1,file2)))
              

              如何使用

              tac('ordered.csv', 'reversed.csv')
              f = open('reversed.csv')
              

              【讨论】:

              • 我认为目标是如何在 Python 中做到这一点。另外,这仅适用于 *Nix 系统,尽管它是一个很好的解决方案。它本质上只是使用 Python 作为提示符来运行 shell 实用程序。
              • 此代码存在当前编写的主要安全漏洞。如果您尝试反转使用mv mycontent.txt $'hello $(rm -rf $HOME) world.txt' 创建的文件,或者类似地使用不受信任的用户提供的输出文件名,该怎么办?如果您想安全地处理任意文件名,则需要更加小心。例如,subprocess.Popen(['tac', file1], stdout=open(file2, 'w')) 是安全的。
              • 现有代码也不能正确处理带有空格、通配符等的文件。
              【解决方案14】:

              感谢@srohde 的回答。它使用“is”运算符检查换行符的小错误,我无法评论 1 信誉的答案。此外,我想管理在外部打开的文件,因为这使我能够嵌入我的 luigi 任务。

              我需要改变的有以下形式:

              with open(filename) as fp:
                  for line in fp:
                      #print line,  # contains new line
                      print '>{}<'.format(line)
              

              我想改成:

              with open(filename) as fp:
                  for line in reversed_fp_iter(fp, 4):
                      #print line,  # contains new line
                      print '>{}<'.format(line)
              

              这是一个修改后的答案,需要一个文件句柄并保留换行符:

              def reversed_fp_iter(fp, buf_size=8192):
                  """a generator that returns the lines of a file in reverse order
                  ref: https://stackoverflow.com/a/23646049/8776239
                  """
                  segment = None  # holds possible incomplete segment at the beginning of the buffer
                  offset = 0
                  fp.seek(0, os.SEEK_END)
                  file_size = remaining_size = fp.tell()
                  while remaining_size > 0:
                      offset = min(file_size, offset + buf_size)
                      fp.seek(file_size - offset)
                      buffer = fp.read(min(remaining_size, buf_size))
                      remaining_size -= buf_size
                      lines = buffer.splitlines(True)
                      # the first line of the buffer is probably not a complete line so
                      # we'll save it and append it to the last line of the next buffer
                      # we read
                      if segment is not None:
                          # if the previous chunk starts right from the beginning of line
                          # do not concat the segment to the last line of new chunk
                          # instead, yield the segment first
                          if buffer[-1] == '\n':
                              #print 'buffer ends with newline'
                              yield segment
                          else:
                              lines[-1] += segment
                              #print 'enlarged last line to >{}<, len {}'.format(lines[-1], len(lines))
                      segment = lines[0]
                      for index in range(len(lines) - 1, 0, -1):
                          if len(lines[index]):
                              yield lines[index]
                  # Don't yield None if the file was empty
                  if segment is not None:
                      yield segment
              

              【讨论】:

                【解决方案15】:

                在处理文件时始终使用with,因为它会为您处理一切:

                with open('filename', 'r') as f:
                    for line in reversed(f.readlines()):
                        print line
                

                或者在 Python 3 中:

                with open('filename', 'r') as f:
                    for line in reversed(list(f.readlines())):
                        print(line)
                

                【讨论】:

                  【解决方案16】:
                  def reverse_lines(filename):
                      y=open(filename).readlines()
                      return y[::-1]
                  

                  【讨论】:

                  【解决方案17】:

                  这样的事情怎么样:

                  import os
                  
                  
                  def readlines_reverse(filename):
                      with open(filename) as qfile:
                          qfile.seek(0, os.SEEK_END)
                          position = qfile.tell()
                          line = ''
                          while position >= 0:
                              qfile.seek(position)
                              next_char = qfile.read(1)
                              if next_char == "\n":
                                  yield line[::-1]
                                  line = ''
                              else:
                                  line += next_char
                              position -= 1
                          yield line[::-1]
                  
                  
                  if __name__ == '__main__':
                      for qline in readlines_reverse(raw_input()):
                          print qline
                  

                  由于文件是以相反的顺序逐个字符读取的,因此即使在非常大的文件上也可以工作,只要单个行适合内存。

                  【讨论】:

                  • 盲目地做 read(1) 会破坏超过 1 个字节的 utf-8 字符
                  【解决方案18】:
                  import re
                  
                  def filerev(somefile, buffer=0x20000):
                    somefile.seek(0, os.SEEK_END)
                    size = somefile.tell()
                    lines = ['']
                    rem = size % buffer
                    pos = max(0, (size // buffer - 1) * buffer)
                    while pos >= 0:
                      somefile.seek(pos, os.SEEK_SET)
                      data = somefile.read(rem + buffer) + lines[0]
                      rem = 0
                      lines = re.findall('[^\n]*\n?', data)
                      ix = len(lines) - 2
                      while ix > 0:
                        yield lines[ix]
                        ix -= 1
                      pos -= buffer
                    else:
                      yield lines[0]
                  
                  with open(sys.argv[1], 'r') as f:
                    for line in filerev(f):
                      sys.stdout.write(line)
                  

                  【讨论】:

                  • 对于大于缓冲区的文件,这似乎会产生错误的输出。据我了解,它不会正确处理跨越您读入的缓冲区大小的块的行。我发布了另一个类似的答案(另一个类似的问题)。
                  • @Darius:啊,是的,我似乎错过了一点。现在应该修好了。
                  • 看起来不错。我仍然更喜欢我自己的代码,因为这确实 O(N^2) 在一个大文件上工作,这都是一行长的。 (在我测试的另一个问题的类似答案中,这导致此类文件的真正速度严重下降。)
                  • 这个问题没有提到性能,所以我不能挑剔正则表达式的性能灾难:P
                  • 更多解释对性能很有用,如果这实际上可以寻求让我们说最后一行并只阅读那篇文章。
                  【解决方案19】:
                  for line in reversed(open("filename").readlines()):
                      print line.rstrip()
                  

                  在 Python 3 中:

                  for line in reversed(list(open("filename"))):
                      print(line.rstrip())
                  

                  【讨论】:

                  • 唉,如果您无法将整个文件放入内存中,这将不起作用。
                  • 另外,虽然发布的代码确实回答了这个问题,但我们应该小心关闭我们打开的文件。 with 语句通常很轻松。
                  • @MichaelDavidWatson:必须先将原始迭代器读入内存,然后在第一个迭代器上反向显示一个新迭代器。
                  • @MichaelDavidWatson:您可以反向读取文件而不将其读入内存,但它并不简单,并且需要大量缓冲区恶作剧以避免大量系统调用浪费。它的性能也会很差(尽管如果文件超过可用内存,比将整个内存读入内存要好)。
                  • @William 抱歉,我如何在迭代文件时使用上述解决方案使用“with open”,然后彻底关闭它?
                  【解决方案20】:

                  在这里你可以找到我的实现,你可以通过改变“buffer”变量来限制ram的使用,有个bug是程序开始打印一个空行。

                  如果没有超过缓冲区字节的新行,内存使用量可能会增加,“泄漏”变量将增加,直到看到新行(“\n”)。

                  这也适用于比我的总内存大的 16 GB 文件。

                  import os,sys
                  buffer = 1024*1024 # 1MB
                  f = open(sys.argv[1])
                  f.seek(0, os.SEEK_END)
                  filesize = f.tell()
                  
                  division, remainder = divmod(filesize, buffer)
                  line_leak=''
                  
                  for chunk_counter in range(1,division + 2):
                      if division - chunk_counter < 0:
                          f.seek(0, os.SEEK_SET)
                          chunk = f.read(remainder)
                      elif division - chunk_counter >= 0:
                          f.seek(-(buffer*chunk_counter), os.SEEK_END)
                          chunk = f.read(buffer)
                  
                      chunk_lines_reversed = list(reversed(chunk.split('\n')))
                      if line_leak: # add line_leak from previous chunk to beginning
                          chunk_lines_reversed[0] += line_leak
                  
                      # after reversed, save the leakedline for next chunk iteration
                      line_leak = chunk_lines_reversed.pop()
                  
                      if chunk_lines_reversed:
                          print "\n".join(chunk_lines_reversed)
                      # print the last leaked line
                      if division - chunk_counter < 0:
                          print line_leak
                  

                  【讨论】:

                    【解决方案21】:

                    前段时间我不得不这样做,并使用了下面的代码。它通过管道连接到外壳。恐怕我已经没有完整的剧本了。如果您使用的是 unixish 操作系统,则可以使用“tac”,但是在例如Mac OSX tac 命令不起作用,请使用 tail -r。下面的代码 sn-p 测试你所在的平台,并相应地调整命令

                    # We need a command to reverse the line order of the file. On Linux this
                    # is 'tac', on OSX it is 'tail -r'
                    # 'tac' is not supported on osx, 'tail -r' is not supported on linux.
                    
                    if sys.platform == "darwin":
                        command += "|tail -r"
                    elif sys.platform == "linux2":
                        command += "|tac"
                    else:
                        raise EnvironmentError('Platform %s not supported' % sys.platform)
                    

                    【讨论】:

                    • 发帖者正在寻找python答案。
                    • 嗯,这是一个 Python 答案,虽然它似乎不完整。
                    • 不是,不是跨平台,使用系统命令=不是pythonic
                    • 发帖者正在寻求“使用 python”的答案,代码 sn-p 确实是用它编写的。但我同意与发布的许多其他解决方案相比,这不是一个很好的解决方案。
                    • sn-p 不够完整,无法评估正确性(调用的其他部分未显示),但将 shell 命令存储在字符串中本身就非常可疑——这很容易除非非常小心,否则会有 shell 注入错误。
                    【解决方案22】:
                    for line in reversed(open("file").readlines()):
                        print line.rstrip()
                    

                    如果你在linux上,你可以使用tac命令。

                    $ tac file
                    

                    您可以在 ActiveState herehere 中找到 2 个食谱

                    【讨论】:

                    • 我想知道 reversed() 是否在迭代之前消耗了整个序列。文档说需要 __reversed__() 方法,但 python2.5 不会抱怨没有它的自定义类。
                    • @muhuk,它可能必须以某种方式缓存它,我怀疑它会以相反的顺序生成一个新列表,然后返回一个迭代器
                    • @Matt:那太荒谬了。它只是从后面到前面——len(L)-1 是后面,0 是前面。你可以想象其余的。
                    • @muhuk:序列没有被有意义地消耗(你可以迭代整个序列,但这并不重要)。 __reversed__ 方法也不是必需的,以前没有这种东西。如果一个对象提供了__len____getitem__,它就可以正常工作(减去一些例外情况,例如dict)。
                    • @Devin Jeanpierre:仅当 readlines() 返回提供__reversed__的对象时?
                    猜你喜欢
                    • 2012-06-11
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多