首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python从文本中解析特定单词

Python从文本中解析特定单词
EN

Stack Overflow用户
提问于 2019-10-13 00:23:49
回答 1查看 493关注 0票数 0

我希望在一篇文章中统计一下“血”这个词的具体提及次数,但我还没有走得太远。我已经尝试了几种方法,最大的收获是能够解析出包含该单词的所有句子。这就是我到目前为止得到的结果,它以退出代码0结束

代码语言:javascript
复制
 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)
EN

回答 1

Stack Overflow用户

发布于 2019-10-13 01:12:31

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

代码语言:javascript
复制
with open('C:\IS452\week7\dracula.txt', 'r', encoding = 'utf=8') as f:
  dracula_txt = f.read().lower()

blood_count = dracula_txt.count("blood")
print(blood_count, "mentions of blood in Dracula")

如果您这样做是为了一些编码挑战,而不允许只使用.count(),那么您可以这样做:

代码语言:javascript
复制
dracula_list = dracula_txt.split("blood") # split text into list separated by occurrences of "blood"
blood_count = len(dracula_list) - 1 # count of occurrences is the length of the list minus one

如果你认为这也是作弊,那么你可以试着这样做:

代码语言:javascript
复制
index = blood_count = 0
while(index != -1):
  try:
    # we get the position of the next occurrence of "blood" in the string, starting from the position of the last occurrence plus one
    index = dracula_txt.index("blood", index) + 1
    blood_count += 1
  except ValueError: # when there aren't any more occurrences of "blood" in the string, we get a ValueError and exit the loop 
    index = -1
print(blood_count, "mentions of blood in Dracula")

或者您可以遍历字符串中的每个字符,检查字符索引后面的子字符串是否与“and”匹配,并相应地递增计数:

代码语言:javascript
复制
blood_count = 0
for i in range(0, len(dracula_txt)):
    if dracula_txt[i:i+5] == "blood":
        blood_count += 1
print(blood_count)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58356123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档