简体   繁体   English

在python中的文本文件中搜索字符串

[英]Searching text file for string in python

I'm using Python to search a large text file for a certain string, below the string is the data that I am interested in performing data analysis on. 我正在使用Python在大型文本文件中搜索某个字符串,该字符串下方是我感兴趣的数据分析对象。

def my_function(filename, variable2, variable3, variable4):
array1 = []

with open(filename) as a:
    special_string = str('info       %d        info =*' %variable3)
    for line in a:
        if special_string == array1:
            array1 = [next(a) for i in range(9)]
            line = next(a)    
            break
        elif special_string != c:
            c = line.strip()

In the special_string variable, whatever comes after info = can vary, so I am trying to put a wildcard operator as seen above. special_string变量中, info =可能会有所不同,因此我试图像上面看到的那样放置通配符。 The only way I can get the function to run though is if I put in the exact string I want to search for, including everything after the equals sign as follows: 我可以运行该函数的唯一方法是,如果我输入了要搜索的确切字符串,包括等号后的所有内容,如下所示:

special_string = str('info         %d       info = more_stuff' %variable3)

How can I assign a wildcard operator to the rest of the string to make my function more robust? 如何为字符串的其余部分分配通配符运算符,以使函数更强大?

Have you thought about using something like this? 您是否考虑过使用类似的东西? Based on your input, I'm assuming the following: 根据您的输入,我假设以下内容:

variable3 = 100000
special_string = str('info         %d       info = more_stuff' %variable3)

import re
pattern = re.compile('(info\s*\d+\s*info\s=)(.*)')
output = pattern.findall(special_string)
print(output[0][1])

Which would return: 哪个会返回:

more_stuff

If your special string always occurs at the start of a line, then you can use the below check (where special_string does not have the * at the end): 如果您的特殊字符串始终出现在行的开头,则可以使用以下检查(其中special_string的末尾没有 * ):

line.startswith(special_string)

Otherwise, please do look at the module re in the standard library for working with regular expressions. 否则的话,看看请模块re在标准库中的使用正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM