| by YoungTimes | No comments

Python-在文件中搜索字符串并获取目标字符串所在的行号

1、检查文件中是否包含目标字符串

为了检测文件中是否包含目标字符串,我们定义了check_if_string_in_file()函数,该函数的参数为:目标文件名和目标字符串。在函数中迭代获取文件的每一行,然后检测该行是否包含目标字符串,如果包含目标字符串,函数返回True;如果文件中所有行都不包含目标字符串,则返回False。

def check_if_string_in_file(file_name, string_to_search):
    """ Check if any line in the file contains given string """
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            if string_to_search in line:
                return True
    return False

假设存在文本文件”sample.txt”,文件内容为:

Hello this is a sample file
It contains sample text
Dummy Line A
Dummy Line B
Dummy Line C
This is the end of file

在文件中检测是否存在”is”字符串。

# Check if string 'is' is found in file 'sample.txt'
if check_if_string_in_file('sample.txt', 'is'):
    print('Yes, string found in file')
else:
    print('String not found in file')

上述代码的执行结果:

Yes, string found in file

由于文件包含字符串”is”,所以函数check_if_string_in_file()返回True。

2、搜索字符串并返回所有包含目标字符串的行及行号

我们对check_if_string_in_file()进行扩展,逐行遍历过程中,增加对行号的计数,如果当前行包含目标字符串,则记录当前行行号和当前行。最后返回所有包含目标字符串的行的内容和行号的Tuple。

def search_string_in_file(file_name, string_to_search):
    """Search for the given string in file and return lines containing that string,
    along with line numbers"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            line_number += 1
            if string_to_search in line:
                # If yes, then add the line number & line as a tuple in the list
                list_of_results.append((line_number, line.rstrip()))
 
    # Return list of tuples containing line numbers and lines where string is found
    return list_of_results

仍以”sample.txt”的文件内容为例,文件的内容如下:

Hello this is a sample file
It contains sample text
Dummy Line A
Dummy Line B
Dummy Line C
This is the end of file

在文件搜索所有包含字符串”is”的行以及所在的行号。

matched_lines = search_string_in_file('sample.txt', 'is')
 
print('Total Matched lines : ', len(matched_lines))
for elem in matched_lines:
    print('Line Number = ', elem[0], ' :: Line = ', elem[1])

程序输出结果如下:

Total Matched lines :  2
Line Number =  1  :: Line =  Hello this is a sample file
Line Number =  6  :: Line =  This is the end of file

3、搜索多个字符串并返回所有包含目标字符串的行及行号

在一个文件中搜索多个字符串,我们不建议针对每个字符串都调用search_string_in_file()函数,因为这相当于每个字符串查询都要重复的打开关闭文件。因此我们实现一个函数,只需要打开文件一次,然后一次性搜索到所有包含每个字符串的行以及行号。

def search_multiple_strings_in_file(file_name, list_of_strings):
    """Get line from the file along with line numbers, which contains any string from the list"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            line_number += 1
            # For each line, check if line contains any string from the list of strings
            for string_to_search in list_of_strings:
                if string_to_search in line:
                    # If any string is found in line, then append that line along with line number in list
                    list_of_results.append((string_to_search, line_number, line.rstrip()))
 
    # Return list of tuples containing matched string, line numbers and lines where string is found
    return list_of_results

仍以”sample.txt”文件为例,假设该文件的内容如下:

This is a sample file
It contains sample text
Dummy Line A
Dummy Line B
Dummy Line C
This is the end of file

在该文件中查找包含字符串”is”或者字符串”what”的行以及行号。

# search for given strings in the file 'sample.txt'
matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what'])
 
print('Total Matched lines : ', len(matched_lines))
for elem in matched_lines:
    print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2])

程序的执行结果如下:

Total Matched lines :  2
Word =  is  :: Line Number =  1  :: Line =  Hello this is a sample file
Word =  is  :: Line Number =  6  :: Line =  This is the end of file

参考链接

https://thispointer.com/python-search-strings-in-a-file-and-get-line-numbers-of-lines-containing-the-string/

发表评论