将文本文件拆分成碎片,然后搜索这些部分中的关键短语

时间:2017-07-06 18:32:02

标签: python dictionary split iteration control-structure

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

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

  2. 使用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

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

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

  4. 我的代码是:

    termz = dict.fromkeys(termz,0)

    它吐出了它的重要性,但它不是它追踪的第一个,最后一个甚至是整个文件 - 我不知道它在做什么。

    最后的印刷声明不合适。 {{1}}行是我发现的将字典值重置为0的方法,但是被注释掉了,因为我不知道如何处理它。基本上,与Python控制结构斗争。如果有人能指出我正确的方向,那就太棒了。

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分:0)

if eachKey in p:
          termz[eachKey] += 1  # might do it
          print(termz)