# coding: utf-8 import re fileName = 'tweets.csv' f = open(fileName, 'r', encoding="utf-8_sig") totalMinutes = 0 while True: line = f.readline() if not line: print('line is false') break if not isinstance(line, str): break # get study time matchStudy = re.search(r'(#mandarin\s*)(#study\s*)(\d+)(\s*)', line) # get date info matchDate = re.search(r'(\d{4})-(\d{2}-\d{2} \d{2}:\d{2}:\d{2})', line) if matchDate and matchStudy: if int(matchDate.group(1)) == 2014: if matchStudy: print('found :',matchStudy.group(3)) totalMinutes += int(matchStudy.group(3)) print('合計時間 ',totalMinutes,'分')