如何查找在已保存的html文件中找到字符串的次数?

我有一个保存的html文件,试图从中查找找到特定字符串的次数。例如, string ='Beautiful days' text =“那些美好的日子令人难忘。我希望每一天都是美好的一天”

预期输出= 2(美丽的日子,美好的一天”

尝试了以下方法: 我尝试使用spacy,但无法做到。谁能告诉我这样做的逻辑吗?

评论
  • o夏樱
    o夏樱 回复

    您还可以使用:

    import re
    
    with open("count_string_in_file.txt") as f:
        html = f.read()
    
    to_match = "beautiful day"
    matches = re.findall(to_match, html)
    print(len(matches))
    # 2
    

    Demo

  • 简暄和
    简暄和 回复

    Consider using lower() and convert both the text that you're looking for and the one that you're looking into:

    x_lst = ['Beautiful days', 'those beautiful days were unforgettable. I wish every day was a beautiful day']
    count = 0
    
    for elem in x_lst:
        if 'Beautiful days'.lower() in elem.lower():
            count += 1
    print(count)