プログラミング初心者です.
現在,pythonを用いてcsv形式のデータの分析を行っております.
前提として,csv,numpy,pandasなどのモジュールを使用しないプログラムを組んでおります.
ある要素を持つデータをすべて抽出した後,特定の曜日(平日)と,
特定の時間帯(8:00~18:00)のみのデータを抜き出し,その中のある要素の平均値を算出するプログラムを自分なりに組んでみたのですが,以下のように長くなってしまいました.
elif部分の重複をなくす,あるいはほかにプログラムを簡潔に示す方法があれば
ご指摘よろしくお願いします.
読み込むcsvファイルは2018/09/01(Sat)から2018/09/30(Sun)です.
python
1import datetime 2 3filepath = 'aaa.csv' 4 5with open(filepath,encoding='shift-jis') as f: 6 7 data2 = [] 8 for line in f: 9 line = line.replace('\n','') 10 data = line.split(',') 11 data2.append(data) 12 13 data3=[] 14 for i in data2: 15 if i[2] =='1': 16 data3.append(i) 17 for i in data3: 18 tmdata=(datetime.datetime.strptime(i[0],'%Y/%m/%d %H:%M')) 19 i[0]=tmdata 20sum=0 21n=0 22a=1 23b=0 24 25for i in data3: 26 if i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*b and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*b\ 27 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 28 sum = sum + int(i[7]) 29 n += 1 30 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*a and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*a\ 31 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 32 sum = sum + int(i[7]) 33 a += 1 34 b += 1 35 n += 1 36 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+1) and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+1)\ 37 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 38 sum = sum + int(i[7]) 39 a += 1 40 b += 1 41 n += 1 42 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+2) and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+2)\ 43 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 44 sum = sum + int(i[7]) 45 a += 1 46 b += 1 47 n += 1 48 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+3) and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+3)\ 49 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 50 sum = sum + int(i[7]) 51 a += 1 52 b += 1 53 n += 1 54 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+4) and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+4)\ 55 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 56 sum = sum + int(i[7]) 57 a += 1 58 b += 1 59 n += 1 60 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+5) and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+5)\ 61 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 62 sum = sum + int(i[7]) 63 a += 1 64 b += 1 65 n += 1 66 elif i[0] < datetime.datetime.strptime('2018/09/01 18:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+6) and i[0] >= datetime.datetime.strptime('2018/09/01 8:00','%Y/%m/%d %H:%M')+datetime.timedelta(days=1)*(a+6)\ 67 and not i[0].strftime('%a') == 'Sat' and not i[0].strftime('%a') == 'Sun': 68 sum = sum + int(i[7]) 69 a += 1 70 b += 1 71 n += 1 72 73print(sum/n) 74
回答2件
あなたの回答
tips
プレビュー