高效地重写简单的python代码

2024-09-29 19:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,可能会重复。也就是说,我需要在代码的不同部分搜索整个日志文件,以查看其中是否存在两种特定的模式。我不能总是在代码开始时立即搜索模式。你知道吗

但基本上,下面是我所拥有的,我正在寻找优化它的方法。假设正在读取的日志文件的大小非常大。你知道吗

    textfile = open(logfile, 'r')
    filetext = textfile.read()
    textfile.close()
    matchesBegin = re.search(BeginSearchDVar, filetext)
    matchesEnd = re.search(EndinSearchDVar, filetext)
    if matchesBegin is not None and matchesEnd is not None:
        LRangeA = SeeIfExactRangeIsFound()
        PatternCount = len(LRangeA)
        LRange = '\n'.join(LRangeA)

我知道这可以优化与选项,但我不知道如何去做。你知道吗


Tags: 文件方法代码renonesearchis模式
1条回答
网友
1楼 · 发布于 2024-09-29 19:21:11

如果您正在寻找优化,请使用^{}。你知道吗

Memory-mapping a file uses the operating system virtual memory system to access the data on the file system directly, instead of using normal I/O functions. Memory-mapping typically improves I/O performance because it does not involve a separate system call for each access and it does not require copying data between buffers – > the memory is accessed directly by both the kernel and the user application.

import mmap
import re

# Create pattern with all, ignore case, and multi line flags.
# search for every instance of `stackoverflow` within a sentence.
pattern =  re.compile( rb'(\.\W+)?([^.]?stackoverflow[^.]*?\.)',
                       re.DOTALL | re.IGNORECASE | re.MULTILINE )

# open file using 'with' which initializes and finalizes an instance
with open( log_file, "r" ) as file:
    # create new instance of mmap
    with mmap.mmap( file.fileno(), # fileno returns file descriptor for IO
                    0, # size in bytes for how much to map (if 0 then entire file)
                    access = mmap.ACCESS_READ # set access flag to read
                  ) as m: # name instance `m`
        matches = pattern.findall( m ) # find all patterns in mapped file
        print( "Matches: " + len( matches ) )
        for match in matches:
            print( match.group(0) )

如果文件确实很大,您可以更改第二个参数(要映射的字节大小)以更好地满足您的需要。你知道吗

相关问题 更多 >

    热门问题