将文本文件拆分为多个部分,然后搜索这些部分中的关键短语

2024-09-29 23:28:27 发布

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

我是Python的新手,我已经是Python的粉丝了。我有一个程序可以执行以下操作:

  1. 打开一个文本文件,其中的文本部分用星号(***)分隔。

  2. 使用split()函数将此文本文件拆分为由这些星号分隔的部分。整个文本文件中的星号行是一致的。

  3. 我希望我的代码遍历这些部分,并执行以下操作:

    • 我有一本字典,上面有“关键短语”的值。字典中每个键的值都是0

    • 代码需要遍历从split创建的每个部分,并检查字典中的键是否在每个部分中找到。如果找到关键字,则该关键字的值将增加1。

    • 一旦代码遍历一个节并计算了该节中的键数并相应地添加了值,它就应该打印出字典键和该设置的计数(值),将值设置为0,然后再次转到从#3开始的下一节文本。

我的代码是:

    from bs4 import BeautifulSoup
   import re
   import time
   import random
   import glob, os
   import string


termz = {'does not exceed' : 0, 'shall not exceed' : 0, 'not exceeding' : 0,
  'do not exceed' : 0, 'not to exceed' : 0, 'shall at no time exceed' : 0,
  'shall not be less than' : 0, 'not less than' : 0}
with open('Q:/hello/place/textfile.txt', 'r') as f:
  sections = f.read().split('**************************************************')
  for p in sections[1:]:
      for eachKey in termz.keys():
        if eachKey in p:
          termz[eachKey] = termz.get(eachKey) + 1
          print(termz)  


#print(len(sections))  #there are thirty sections      

        #should be if code encounters ***** then it resets the counters and just moves on....
        #so far only can count the phrases over the entire text file....

#GO BACK TO .SPLIT()
# termz = dict.fromkeys(termz,0) #resets the counter

它吐出了它的计数,但它不是第一个,最后一个,甚至不是它跟踪的整个文件-我不知道它在做什么。你知道吗

结尾的打印语句不合适。termz = dict.fromkeys(termz,0)行是我发现的一个方法,它可以将字典的值重置为0,但是被注释掉了,因为我不知道如何处理这个问题。从本质上讲,与Python控制结构斗争。如果有人能给我指出正确的方向,那就太棒了。你知道吗


Tags: the代码in文本import字典not星号
2条回答

你的密码很接近。见以下评论:

termz = {
    'does not exceed': 0,
    'shall not exceed': 0,
    'not exceeding': 0,
    'do not exceed': 0,
    'not to exceed': 0,
    'shall at no time exceed': 0,
    'shall not be less than': 0,
    'not less than': 0
}

with open('Q:/hello/place/textfile.txt', 'r') as f:
    sections = f.read().split('**************************************************')

    # Skip the first section. (I assume this is on purpose?)
    for p in sections[1:]:
        for eachKey in termz:
            if eachKey in p:
                # This is simpler than termz[eachKey] = termz.get(eachKey) + 1
                termz[eachKey] += 1

        # Move this outside of the inner loop
        print(termz)

        # After printing the results for that section, reset the counts
        termz = dict.fromkeys(termz, 0)

编辑

输入和输出示例:

input = '''
Section 1:

This section is ignored.
does not exceed
**************************************************
Section 2:

shall not exceed
not to exceed
**************************************************
Section 3:

not less than'''

termz = {
    'does not exceed': 0,
    'shall not exceed': 0,
    'not exceeding': 0,
    'do not exceed': 0,
    'not to exceed': 0,
    'shall at no time exceed': 0,
    'shall not be less than': 0,
    'not less than': 0
}

sections = input.split('**************************************************')

# Skip the first section. (I assume this is on purpose?)
for p in sections[1:]:
    for eachKey in termz:
        if eachKey in p:
            # This is simpler than termz[eachKey] = termz.get(eachKey) + 1
            termz[eachKey] += 1

    # Move this outside of the inner loop
    print(termz)

    # After printing the results for that section, reset the counts
    termz = dict.fromkeys(termz, 0)

# OUTPUT:
# {'not exceeding': 0, 'shall not exceed': 1, 'not less than': 0, 'shall not be less than': 0, 'shall at no time exceed': 0, 'not to exceed': 1, 'do not exceed': 0, 'does not exceed': 0}
# {'not exceeding': 0, 'shall not exceed': 0, 'not less than': 1, 'shall not be less than': 0, 'shall at no time exceed': 0, 'not to exceed': 0, 'do not exceed': 0, 'does not exceed': 0}
if eachKey in p:
          termz[eachKey] += 1  # might do it
          print(termz)

相关问题 更多 >

    热门问题