with open("test.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
print(line)
with open("test.txt", "w", encoding="utf-8") as f:
f.write("각 줄마다 번호를 적은 파일입니다.\n")
for i in [1,2,3,4,5]:
f.write(f"이것은 {i}번째 줄입니다.\n")
with open("test.txt", "w", encoding="utf-8") as f:
f.write("안녕, 스파르타!")
PC카톡에서 채팅방을 열고 메뉴 -> 대화내용 -> 대화내용 내보내기로 TXT 파일 준비
각 라인을 읽어 text 변수에 저장하기
text = ""
# 파일 이름은 맞게 바꿔주세요!
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
text+=line
print(text)
wordcloud 패키지 설치하기.
font_path = 'C:\Windows\Fonts\malgunbd.ttf'
from wordcloud import WordCloud
text = ""
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
text += line
font_path = 'C:/Windows/Fonts/CoreGTM3.otf'
wc = WordCloud(font_path=font_path, background_color="white", width=600, height=400)
wc.generate(text)
wc.to_file("result.png")
from wordcloud import WordCloud
text = ""
# 파일 이름은 맞게 바꿔주세요!
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
text += line
print(text)
# font_path = 'C:/Windows/Fonts/CoreGTM3.otf'
#
# wc = WordCloud(font_path=font_path, background_color="white", width=600, height=400)
# wc.generate(text)
# wc.to_file("result.png")
from wordcloud import WordCloud
text = ""
# 파일 이름은 맞게 바꿔주세요!
with open("kakaotalk.txt", "r", encoding="utf-8") as file:
lines = file.readlines()
for line in lines:
if '] [' in line:
text += line.split('] ')[2].replace('ㅋ','').replace('ㅠ','').replace('ㅜ','').replace('사진\n','').replace('이모티콘\n','').replace('삭제된 메시지입니다','')
font_path = 'C:/Windows/Fonts/CoreGTM3.otf'
wc = WordCloud(font_path=font_path, background_color="white", width=600, height=400)
wc.generate(text)
wc.to_file("result.png")
MASK 이미지를 준비해서 만든다.
from PIL import Image
import numpy as np
mask = np.array(Image.open('cloud.png'))
wc = WordCloud(font_path=font_path, background_color="white", mask=mask)
wc.generate(text)
wc.to_file("result_masked.png")