Skip to main content
 首页 » 编程设计

python之如何查找文件/字符串中字符串之间的字符串数之python

2025年05月04日24thcjp

**************************************解决方案*********** *****************

经过大量测试和一些调整后,我成功地编写了一个工作代码!

我正在与所有人分享它,以防有人有兴趣执行与我相同的操作。 对于所有提供帮助的人 - 我谢谢你们! :)

stringToSearchIn = open('FileName.py').read() 
 
def findBetween(file, firststring, laststring, findstring): 
    start = 0 
    countfinal = 0 
    while True: 
        try: 
            start = file.index(firststring, start) 
        except: 
            break 
        try: 
            end = file.index(laststring, start) 
            count = file[start:end].count(findstring) 
            countfinal = count + countfinal 
            start = end 
        except: 
            break 
     return countfinal 
 
print findBetween(stringToSearchIn, "example", "file", "letters") 

*********************************最终解决方案************ ***************

我已经尝试解决这个问题很长一段时间了,我相信我已经把复杂的事情放在心上了。 我写起来甚至有点复杂,但我会尽力而为。如果有什么不清楚的地方,请随时提问!

请不要为我编写代码。我是来学习的,不是来抄袭的:)

例如:

#This is the entire text I want to scan 
      s = open('test.py').read() 
#I want to go through the entire file and find the string between these two strings: 
     stringStartToSearch = "example"  
     stringEndToSearch = "file" 
#Next, I want to count the number of times a certain string is located  
#between the previously found string. 
     stringSearch = "letters" 

为了进一步说明,假设这是在“test.py”文件中找到的字符串:

#An example text that I have many letters in, just to give and example for a file. 
#It's an example with many letters that I made especially for this file test. 
#And these are many letters which should not be counted 

如您所见,单词“letters”在该文件中出现了 3 次,但在“example”和“file”之间只有 2 次 。这就是我要计算的。

有谁知道实现此目的的高效 pythonic 方法?

非常感谢!

给你sabbahillel

脚本确实在 2 个给定字符串之间找到了正确的字符串,但是在找到之后停止。我需要它继续搜索整个文件,并且在找到后不会停止。 此外,在我找到这两个字符串之间的字符串后,我需要遍历它并计算某个单词显示的次数。这可以通过哪个命令实现?

file = open('testfile.py').read() 
 
def findBetween(file, firstWord, secondWord): 
        start = file.index(firstWord)+len(firstWord) 
        end = file.index(secondWord, start) 
        return file[start:end] 
 
print findBetween(file, "example", "file") 

请您参考如下方法:

让我们假设您有您提供的字符串列表。

Python Lists

列表.索引(x)

返回值为 x 的第一项在列表中的索引。如果没有这样的项目是一个错误。

获取开始索引和结束索引。如果 begin 和 end 都存在,并且 end 的索引大于 start 的索引,则只需使用 start 和 end 索引上的范围进行处理即可获取所需的元素。

当然,如果您有开始指示符但到达列表末尾没有结束指示符(作为必须处理的错误情况的示例),您必须进行适当的错误检查并决定要做什么)

请注意,list.index() 会查找第一个出现的起始字符串。如果还有更多,则从第一次出现结束字符串开始范围,然后再做一次。这可以在适当的 do ... while 循环中完成,while 循环检查是否再次出现起始字符串。

请注意,如果列表中再次出现起始字符串,则不会将其视为重新开始,而只是另一个条目。

mylist = ('string' 'start' 'string' 'start' 'string' 'end' 'string) 

将处理

('start' 'string' 'start' 'string' 'end') 

因此我们现在

start = 0 
 
while True: 
    try: 
        start = mylist[start:].index(firststring) 
    except: 
        # index did not find start string. nothing to do, force exit 
        break 
    try: 
        end = mylist[start:].index(laststring) 
        count = mylist[start:end].count(findstring) 
        # process findstring 
        start = end # set up for the next loop 
    except: 
        # index did not find end string but did find start 
        count = mylist[start:].count(findstring) 
        # process findstring 
        break # reached the end of the list, exit the while 

现在您有了开始和结束索引

索引、切片和矩阵

因为列表是序列,索引和切片对列表的工作方式与对字符串的工作方式相同。所以只需使用 list[a:b].count(string) 和适当的切片指示符..

list.count(对象)

返回 obj 在列表中出现的次数