Day 31 - Flash Card Project

這天的project是做學習閃卡,不過我是做的是中文學習閃卡而非使用Angela給的csv(法文)。所以需先自己做一些資料清洗(裡面可能包含非中文字)。

以下內容說明主要為資料清洗部分,若有些步驟可以做整合調整,請自行斟酌。

以下為上方步驟程式碼實現:

import pandas as pd
import re

def is_chinese(uchar):
    zhPattern = re.compile(u'[\\u4e00-\\u9fa5]+')
    match = zhPattern.search(uchar)
    if match:
        return True

with open('words.txt', encoding='utf-8') as f:
    data = f.readlines()

right_word = []
for letter in [word.split(" ")[0] for word in data]:
    if is_chinese(letter):
        right_word.append(letter)

df = pd.DataFrame(right_word)
df.to_csv('right_word.csv', header=False, index=False, encoding='utf_8_sig')

Day 32 - Happy Birthday Project

這天的project是將不同人的名字、電子信箱帳號、出生年、月、日給記錄在birthdays.csv,之後再根據今天日期判斷檔案裡面是否有人今天生日。若是符合條件便隨機從letter_templates資料夾裡選取寄信內容的模板,發送電郵祝賀。

以下為程式碼實現(我與老師的方法在一些地方有些微的不同,僅供大家參考):

import pandas as pd
import smtplib
import datetime as dt
import os
import random

MY_USERNAME = 'Your mail account'
MY_PASSWORD = 'Your mail password'

now = dt.datetime.now()
info = pd.read_csv('birthdays.csv')

test_name = info[(info.month == now.month) & (info.day == now.day)].name
test_mail = info[(info.month == now.month) & (info.day == now.day)].email
match_lst = [i for i in zip(test_name, test_mail)]

for person in match_lst:
    random_letter = random.choice(os.listdir('letter_templates'))
    with open(f'letter_templates/{random_letter}') as f:
        data = f.read()
    data = data.replace('[NAME]', person[0])

    try:
        with smtplib.SMTP('smtp.gmail.com') as connection:
            connection.starttls()
            connection.login(user=MY_USERNAME, password=MY_PASSWORD)
            connection.sendmail(from_addr=MY_USERNAME, to_addrs=person[1], msg=f"Subject:Happy Birthday to {person[0]}\\n\\n{data}")
    except:
        print("Send mail failed!")
    else:
        print('Send mail sucessfully!')