파이썬을 사용하여 텍스트 파일에서 단어의 빈도를 찾는 방법은 무엇입니까? 하지만 사용자는 입력 단어를 제공해야합니다.

슈레 야 레디

내 목표 : 텍스트 파일에 사용자가 입력 한 단어의 빈도를 세는 것 (파이썬에서) 시도했지만 파일에있는 모든 단어의 빈도를 제공합니다. 단어의 빈도를 제공하도록 수정하려면 어떻게해야합니까? 사용자가 입력 했습니까?

from collections import Counter
word=input("Enter a word:")
def word_count(test6):
        with open('test6.txt') as f:
                return Counter(f.read().split())

print("Number of input words in the file :",word_count(word))

순진한 질문 일 수 있지만 이제 막 코딩을 시작 했으니 답해주세요. 미리 감사드립니다.

케데 라크

파일에서 단어의 빈도를 찾으려면 str.join다음을 사용 하여 파일의 모든 행을 결합한 다음 사용하십시오 str.count.

def word_count(word):
    with open('test6.txt') as f:
            return ''.join(f).count(word)


print("Number of words in the file :", word_count(input('give me a word')))

또한 텍스트의 단어 수에 사용할 수 있습니다.

def word_count(word):
        with open('test6.txt') as f:
                return f.read().count(word)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관