如何查找 file/string - python 中字符串之间的字符串数
How to find the number of strings between strings in a file/string - 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"
为了进一步说明,假设这是在 "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次[=48] =].这就是我要数的。
有谁知道实现此目的的高效 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")
使用正则表达式查找:
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
让我们假设您有您提供的字符串列表。
list.index(x)
Return 列表中第一项值为 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) 和适当的切片指示符..
list.count(obj)
Returns obj 在列表中出现的次数
**************************************解决方案********* *******************
经过大量测试和一些调整后,我成功地编写了一个工作代码!
我正在与大家分享它,以防有人对执行与我相同的操作感兴趣。 对于所有提供帮助的人 - 我谢谢你们! :)
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次[=48] =].这就是我要数的。
有谁知道实现此目的的高效 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")
使用正则表达式查找:
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
让我们假设您有您提供的字符串列表。
list.index(x)
Return 列表中第一项值为 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) 和适当的切片指示符..
list.count(obj)
Returns obj 在列表中出现的次数