如果这真的是基于行的(不需要真正的XML解析器,最好的解决方案),^{}可以在这里提供帮助。在
mmap文件,然后对结果对象调用.rfind('\n')(如果您确实希望文件前面是非空行,而不是后面的空行,则可能需要进行调整以新行结尾的文件)。然后你可以把最后一行切掉。如果需要就地修改文件,可以调整文件的大小,去掉(或添加)与所切片的行和新行之间的差异相对应的字节数,然后写回新行。避免读取或写入超出需要的文件。在
示例代码(如果我出错,请评论):import mmap
# In Python 3.1 and earlier, you'd wrap mmap in contextlib.closing; mmap
# didn't support the context manager protocol natively until 3.2; see example below
with open("large.XML", 'r+b') as myfile, mmap.mmap(myfile.fileno(), 0, access=mmap.ACCESS_WRITE) as mm:
# len(mm) - 1 handles files ending w/newline by getting the prior line
# + 1 to avoid catching prior newline (and handle one line file seamlessly)
startofline = mm.rfind(b'\n', 0, len(mm) - 1) + 1
# Get the line (with any newline stripped)
line