Searching for a string in a large text file - profiling various methods in python

后端 未结 6 546
傲寒
傲寒 2020-12-02 05:54

This question has been asked many times. After spending some time reading the answers, I did some quick profiling to try out the various methods mentioned previously...

6条回答
  •  伪装坚强ぢ
    2020-12-02 06:35

    You could also try

    with open('input.txt') as f:
        # search_str is matched against each line in turn; returns on the first match:
        print search_str in f
    

    with search_str ending with the proper newline sequence('\n' or '\r\n'). This should use little memory, as the file is read progressively. It should also be quite fast, since only part of the file is read.

提交回复
热议问题