python3 wordcount txt파일 읽어서 단어개수 출력

#-*- coding: utf-8 -*-
import time
import re
 
regex = re.compile('[a-z]+|[A-Z]+|[^0-9]')
t1 =time.time()
 
f = open("읽고싶은 텍스트파일.txt",'r')
text=f.read()
 
wordss=text.split()
words=regex.findall(str(wordss))
word_counts=dict()
 
 
for word in words:
word_counts[word]=word_counts.get(word,0)+1
 
for word,count in word_counts.items():
print(word,count)
 
f.close()
t2 =time.time()
print(t2-t1)

정규식과 딕셔너리 자료형을 이용하여 영어단어를 찾아 개수를 출력해주는 코드.
실행시간도 출력해준다.

100MB 정도 되는 텍스트 파일을 우분투 환경에서 실행 시킨 결과 이미지
대략 40초 정도 걸렸다.

보다 정교하게 걸러내기 위해서는 코드를 손봐야한다.

댓글

이 블로그의 인기 게시물

About JVM Warm up

About idempotent

About Kafka Basic

About ZGC

sneak peek jitpack

Spring Boot Actuator readiness, liveness probes on k8s

About Websocket minimize data size and data transfer cost on cloud

About G1 GC

대학생 코딩 과제 대행 java, python, oracle 네 번째