我是Python的新手,我已经是Python的粉丝了。我有一个程序可以执行以下操作:
打开一个文本文件,其中的文本部分用星号(***
)分隔。
使用split()
函数将此文本文件拆分为由这些星号分隔的部分。整个文本文件中的星号行是一致的。
我希望我的代码遍历这些部分,并执行以下操作:
我有一本字典,上面有“关键短语”的值。字典中每个键的值都是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控制结构斗争。如果有人能给我指出正确的方向,那就太棒了。你知道吗
你的密码很接近。见以下评论:
编辑
输入和输出示例:
相关问题 更多 >
编程相关推荐