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

2024-04-26 14:52:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我会和大家分享,以防有人对我的表演感兴趣。 感谢所有帮助过我的人-谢谢!:)

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"

为了进一步澄清,假设这是在测试.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次。这就是我想数的。你知道吗

有人知道一个有效的Python方法来实现这一点吗?你知道吗

非常感谢!你知道吗

为您萨巴伊勒

脚本确实在两个给定字符串之间找到了正确的字符串,但是,在找到它之后就会停止。我需要它继续搜索整个文件,而不是在找到后停止。 另外,在我找到这两个字符串之间的字符串之后,我需要遍历它并计算某个单词显示了多少次。用什么命令才能做到这一点?你知道吗

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")

Tags: theto字符串pyreadindexexamplecount
2条回答

假设你有你给它的字符串列表。你知道吗

Python Lists

你知道吗列表.索引(十)

返回值为x的第一个项的列表中的索引。如果没有这样的项,则为错误。你知道吗

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

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

请注意列表.索引()查找开始字符串的第一个匹配项。如果有更多,则从第一个出现的结束字符串开始,然后再次执行。这可以在适当的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在列表中出现的次数计数

使用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

相关问题 更多 >