Python从文本中解析某些单词

问题描述 投票:0回答:1

我希望在文本中算出具体提到血这个词,但距离我还很远。我已经尝试了几件事,而我得到的最远的结果是能够解析出包含该单词的所有句子。这是我到目前为止的内容,以退出代码0完成>

 infile = open('C:\IS452\week7\dracula.txt', 'rt', encoding = 'utf=8')
    dracula_lines = infile.readlines()
    infile.close()

    for blood_lines in dracula_lines:

    accumulator = blood_lines.strip()


    dracula_lines.count("blood")
    if "blood" in dracula_lines:
        print("blood") in str(newText[blood_lines + 1])
    #print(blood_lines)

    blood_lines = accumulator.split("blood")
    newText = ("There are this many mentions of blood in Dracula", blood_lines)
      print(newText)

我希望在文本中算出具体提到血这个词,但距离我还很远。我已经尝试了几件事,而我得到的最远的结果是能够解析出所有句子...

python-3.x string parsing word accumulator
1个回答
0
投票

您可以直接在字符串上使用.count()

© www.soinside.com 2019 - 2024. All rights reserved.