전체 전처리 코드(주피터 노트북)
90년대 뉴스의 경우, 한자 → 한글 변환후 사용 예정, 라이브러리 특성상 100% 변환이 불가한 경우 직접 목록을 가진 다음 전처리 등을 고려하기
# 한자 -> 한글로 변환
text_headline = hanja.translate(text_headline, 'substitution')
# 한자 -> 한글로 변환
text_sentence = hanja.translate(text_sentence, 'substitution')
# CSV 작성
writer.write_row([time, category_name, text_company, text_headline, text_sentence, content_url])
인코딩 utf-8로 변경 (csv 저장시 간단하게 설정 가능)
뉴스 합치기
# https://stackoverflow.com/questions/13613336/how-do-i-concatenate-text-files-in-python
import glob
import shutil
with open(f'{cur}.csv','wb') as wfd:
for f in [f'{cur}_네이버뉴스_부동산.csv', f'{cur}_네이버뉴스_증권.csv']:
with open(f,'rb') as fd:
shutil.copyfileobj(fd, wfd)
중복 제거, 파일 삭제
import pandas as pd
file_name = f'{cur}_concat.csv'
file_name_output = f'{cur}.csv'
df = pd.read_csv(file_name, sep="\t or ,", encoding="utf_8", engine="python")
# Notes:
# - the `subset=None` means that every column is used
# to determine if two rows are different; to change that specify
# the columns as an array
# - the `inplace=True` means that the data structure is changed and
# the duplicate rows are gone
df.drop_duplicates(subset=None, inplace=True)
# Write the results to a different file
df.to_csv(file_name_output, index=False)
# 중복 제거되지 않은 이전 파일 삭제
import os
os.remove(f'{cur}_concat.csv')
키워드 추출후 csv의 새로운 Column에 키워드 추출 목록 저장, 20개 할 예정