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

如何在文件/string-python中查找字符串之间的字符串数,python,string,file,python-3.x,find,Python,String,File,Python 3.x,Find,***********************************解决方案**************************** 经过大量的测试和一些调整,我已经成功地编写了一个工作代码 我会和每个人分享它,以防有人对我的表演感兴趣。 感谢所有帮助过我的人——我感谢你们!:) *********************************最终解决方案*************************** 我试图解决这个问题已经有相当长的一段时间了,我相信在我的头脑中我已经把事情复杂化了

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

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

我会和每个人分享它,以防有人对我的表演感兴趣。 感谢所有帮助过我的人——我感谢你们!:)

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

我试图解决这个问题已经有相当长的一段时间了,我相信在我的头脑中我已经把事情复杂化了。 这对我来说甚至有点复杂,但我会尽我最大的努力。如果有什么不清楚的地方,尽管问吧

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

例如:

#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”文件中找到的字符串:

如您所见,“字母”一词在该文件中出现了3次,但在“示例”和“文件”之间仅出现了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")
使用regexp查找:

import re

example = """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"""

found_lines = re.findall('.+example.+letters.+file.+', example)

result = {}
for line in found_lines:
    example_word = line.find('example') + len('example')
    file_word = line.find('file', example_word)
    result[line] = file_word - example_word

print result

让我们假设您有您给出的字符串列表

列表.索引(x)

返回值为x的第一项列表中的索引。如果没有此类项目,则为错误

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

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

请注意,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)和适当的切片指示符即可

列表计数(obj)


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

签出和字符串切片将是一个良好的开始。此外,如果文本中有连续的示例或文件,该怎么办?我的意思是像
。。。例子。。。信。。。例子。。。信。。。文件
如果测试字符串是
示例文件字母file
,结果应该是什么?@Lafexlos-将有连续的示例和文件,我想计算在它们之间找到“字母”的所有时间。因此,您希望获得第一个
示例
和最后一个
文件
之间的所有
字母,谢谢你的帮助。“result”给出在“text”中找到的整个字符串,不显示单词“letters”位于“example”和“files”之间的次数。不幸的是,这不是我想要的,非常感谢!:)谢谢!这帮助我解决了我第一次在字符串之间搜索字符串的问题。现在我需要以某种方式创建第二部分。我已经更新了我的主要帖子。请查看“为您sabbahillel”下的部分。谢谢:)
('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