0
텍스트 파일에서 단어의 최소 및 최대 개수를 묻는 질문이 있습니다. 나는 5 가지 질문 중 3 가지를 끝내고 나머지 2 가지는 최소값과 최대 값을 요구하고 있습니다. 여기 내 코드는 : 당신의 도움을 주셔서 감사합니다입력 파일에서 문장 사이의 최대 및 최소 단어 수 찾기
lines, blanklines, sentences, words = 0, 0, 0, 0,
print '-' * 50
full_text = 'input.txt'
empty_text = 'output.txt'
text_file = open(full_text, 'r')
out_file = open(empty_text, "w")
for line in text_file:
print line
lines += 1
if line.startswith('\n'):
blanklines += 1
else:
# assume that each sentence ends with . or ! or ?
# so simply count these characters
sentences += line.count('.') + line.count('!') + line.count('?')
# create a list of words
# use None to split at any whitespace regardless of length
# so for instance double space counts as one space
# word total count
words += len(line.split())
average = float(words)/float(sentences)
text_file.close()
out_file.close()
######## T E S T P R O G R A M ########
print
print '-' * 50
print "Total number of sentences in the input file : ", sentences
print "Total number of words in the input file : ", words
print "Average number of words per sentence : ", average
내가 제안 https://docs.python.org/2.7/library/re.html#re.split을 참조하십시오. –