웹크롤링 및 전처리 과정
https://www.notion.so/b45ca21c3dc4417ea537750fc7185809
웹크롤링 및 전처리 과정이 끝난 단어를 현재 시간와 함께 csv파일 생성 or 기존 파일에 데이터를 추가한다.
처음 파일이 생성될 때만 항목 컬럼을 넣고 싶어 방법을 생각해본 결과 try , except 문법 활용이 가장 깔끔해 보였다.
def save_to_csv(**context):
from datetime import datetime ###
word_counted = context['task_instance'].xcom_pull(key='processed_count_value', task_ids='Pre_processing')
word_counted.append(datetime.now())
try:
f = open('csv_mysql.csv', 'r')
with open('csv_mysql.csv', 'a',newline='', encoding='UTF-8') as f:
write = csv.writer(f)
write.writerow(word_counted)
except:
#처음 작업 시 -> 항목 컬럼 추가
fields = ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','Data_Time']
with open('csv_mysql.csv', 'w',newline='', encoding='UTF-8') as f:
write = csv.writer(f)
write.writerow(fields)
write.writerow(word_counted)