아래 프로그램은 의도 한대로 작동합니다. 이것은 사용자가 읽을 텍스트 파일을 선택할 수있는 대화 상자를 열어 여러 텍스트 파일을 읽으면 "List_Of_Words.txt"라는 새로운 출력 파일에 결과를 출력합니다.누적 합계 - Python
내가 겪고있는 문제는 프로그램이 여러 개의 텍스트 파일을 읽고 출력 파일에 추가 할 때 출력 파일에 총계를 포함시키는 방법을 알아낼 수 없다는 것을 알 수 있습니다. 발견 한 단어의 수. 예를 들어, 3 개의 텍스트 파일을 읽고 각 텍스트 파일에서 찾은 단어를 제공하고 각 단어 옆에 단어가 나타나는 횟수가 표시되지만 총 단어 수를 말해야합니다. 출력 파일의 맨 아래에있는 모든 텍스트 파일에서 찾습니다.
proclamation: 7
constitution: 1
people: 6
authority: 2
strong: 1
freedom: 3
rebellion: 7
mankind: 1
emancipation: 2
slaves: 3
문서 된 .txt 노예 해방 령 (1863년 1월 1일) -/Python27/에이브 러햄 링컨 :
문서 이름 : C 나는 3 개 텍스트 파일에 대해 프로그램을 실행할 때 나는 무엇을 얻을
이름 : C :/Python27/앤드류 잭슨 (Andrew Jackson) - 두 번째 취임 연설 (1833년 3월 4일)
leaders: 1
constitution: 2
liberty: 4
mankind: 1
society: 1
countrymen: 1
wisdom: 1
responsibility: 1
federal: 2
impoverished: 1
country: 3
happiness: 2
community: 1
world: 2
people: 9
citizens: 3
blessings: 1
contribute: 1
republic: 2
문서 이름이 .txt : C : /Python27/Gettysburg.txt
liberty: 1
nation: 5
world: 1
brave: 1
people: 4
freedom: 1
은 내가 마지막에 "각 단어를 기반으로 발견되는 모든 단어의 총 수 :"입니다에서 찾고 있어요 모든 파일
다음에서 + 단어 + 단어의 빈도하면 프로그램의 코드입니다 :
from sys import argv
import sys
from string import punctuation
from collections import *
import Tkinter, tkFileDialog
keyWords = ['God', 'Nation', 'nation', 'USA', 'Creater', 'creater', 'Country', 'Almighty',
'country', 'People', 'people', 'Liberty', 'liberty', 'America', 'Independence',
'honor', 'brave', 'Freedom', 'freedom', 'Courage', 'courage', 'Proclamation',
'proclamation', 'United States', 'Emancipation', 'emancipation', 'Constitution',
'constitution', 'Government', 'Citizens', 'citizens', 'love', 'Love', 'Strong',
'strong', 'Happiness', 'happiness', 'Dignity', 'dignity', 'Motivation', 'motivation',
'Strength', 'strgenth', 'authority', 'rebellion', 'slave', 'slaves', 'contribute',
'countrymen', 'leader', 'leaders', 'impoverished', 'community', 'society', 'republic',
'democrat', 'democracy', 'wisdom', 'world', 'mankind', 'responsibility', 'blessing',
'blessings', 'federal']
fileDict = {}
print "Do you know the location of the file(s)?"
answer = raw_input("> ")
if answer.lower() == "yes":
file_path = tkFileDialog.askopenfilename()
elif answer.lower() == "no":
print "\nPlease locate the file first before running program\n"
print "Program will now close"
sys.exit()
if file_path:
print "Text file to import and read:", file_path
print "\nReading file..."
word_freq = {}
text_file = open(file_path, 'r')
all_lines = text_file.readlines()
text_file.close()
print "\nFile read finished!\n"
for line in all_lines:
for word in line.split():
word = word.strip(punctuation).lower()
if word in word_freq:
word_freq[word] += 1;
else:
word_freq[word] = 1;
fileDict[file_path] = word_freq
print "Writing sum of results to: List_Of_Words.txt"
output_file = open("List_Of_Words.txt", "a")
for fileName in fileDict:
output_file.write("\nDocument Name: %s\n\n" % (fileName))
for word in fileDict[fileName]:
if word in keyWords:
output_file.write("%s: %3d\n" % (word, word_freq[word]))
output_file.close()
당신은/공정에게 단어가 발생할 때마다 이전 또는 파일의 시작 부분에 사전을 output_file.close() – Sam
선언 한 후, 다음을 업데이트합니다. 마지막으로, 사전을 반복하고 그 결과를 파일에 쓰십시오. –