python/Django

[Django] 엑셀 읽고 딕셔너리(Dictionary) 형태로 Django 페이지에 불러오기

http://portfolio.wonpaper.net 2022. 9. 2. 12:39

엑셀파일을 읽고 (pandas 활용) Dictionary 형태로 변환시키고 Django 페이지에 불러오는 방법 알아보자.

pandas_excel_data.xlsx
0.01MB

잠깐 주피터 노트북으로 따로 돌려보자.

import pandas as pd
df = pd.read_excel("pandas_excel_data.xlsx")
df.head()
   grade            name                                     email  value
0      1    Belle Hunter                Donec@luctusfelispurus.net     84
1      4     Hadley York            ipsum.non@tempusnonlacinia.net     97
2      5   Maryam Graves           ut.pellentesque.eget@duinec.org      3
3      4  Armando Nguyen  Nunc.ullamcorper.velit@posuerecubilia.ca     25
4      5      Dean House    cursus.et.eros@ametluctusvulputate.org      1
grade_dic = {}

total_row_num = len(df.index)
print(total_row_num)
100
df.loc[1]
grade                                 4
name                        Hadley York
email    ipsum.non@tempusnonlacinia.net
value                                97
Name: 1, dtype: object
df.loc[1,'name']
'Hadley York'
for i in range(total_row_num):
    data = df.loc[i]
    if not data['grade'] in grade_dic.keys():   # grade_dic_keys() grade 열의값으로 등록 [1,4,5,2,3]
        grade_dic[data['grade']] = [data['value']]
        #print(grade_dic)
    else:
        grade_dic[data['grade']].append(data['value'])
    print(grade_dic)
    #print(grade_dic.keys())
{1: [84]}
{1: [84], 4: [97]}
{1: [84], 4: [97], 5: [3]}
{1: [84], 4: [97, 25], 5: [3]}
{1: [84], 4: [97, 25], 5: [3, 1]}
{1: [84], 4: [97, 25], 5: [3, 1], 2: [70]}
{1: [84], 4: [97, 25, 1], 5: [3, 1], 2: [70]}
{1: [84], 4: [97, 25, 1, 38], 5: [3, 1], 2: [70]}
{1: [84], 4: [97, 25, 1, 38], 5: [3, 1], 2: [70, 56]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1], 2: [70, 56]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1, 28], 2: [70, 56]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1, 28], 2: [70, 56], 3: [2]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1, 28], 2: [70, 56, 71], 3: [2]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1, 28], 2: [70, 56, 71], 3: [2, 24]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1, 28], 2: [70, 56, 71, 37], 3: [2, 24]}
{1: [84, 17], 4: [97, 25, 1, 38], 5: [3, 1, 28], 2: [70, 56, 71, 37], 3: [2, 24, 87]}
{1: [84, 17], 4: [97, 25, 1, 38, 8], 5: [3, 1, 28], 2: [70, 56, 71, 37], 3: [2, 24, 87]}
{1: [84, 17], 4: [97, 25, 1, 38, 8], 5: [3, 1, 28], 2: [70, 56, 71, 37, 53], 3: [2, 24, 87]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30], 5: [3, 1, 28], 2: [70, 56, 71, 37, 53], 3: [2, 24, 87]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30], 5: [3, 1, 28], 2: [70, 56, 71, 37, 53, 18], 3: [2, 24, 87]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30], 5: [3, 1, 28], 2: [70, 56, 71, 37, 53, 18], 3: [2, 24, 87, 65]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30], 5: [3, 1, 28], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65, 83]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30, 35], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65, 83]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30, 35], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65, 83, 28]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30, 35], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65, 83, 28, 68]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30, 35], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83], 3: [2, 24, 87, 65, 83, 28, 68, 69]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30, 35], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69]}
{1: [84, 17], 4: [97, 25, 1, 38, 8, 30, 35, 61], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69]}
{1: [84, 17, 37], 4: [97, 25, 1, 38, 8, 30, 35, 61], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69]}
{1: [84, 17, 37], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18], 5: [3, 1, 28, 52], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69]}
{1: [84, 17, 37], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18], 5: [3, 1, 28, 52, 40], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69]}
{1: [84, 17, 37], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18], 5: [3, 1, 28, 52, 40], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18], 5: [3, 1, 28, 52, 40], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95], 5: [3, 1, 28, 52, 40], 2: [70, 56, 71, 37, 53, 18, 83, 76], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95], 5: [3, 1, 28, 52, 40], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95], 5: [3, 1, 28, 52, 40, 91], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95], 5: [3, 1, 28, 52, 40, 91], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33]}
{1: [84, 17, 37, 45], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33]}
{1: [84, 17, 37, 45, 23], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33]}
{1: [84, 17, 37, 45, 23, 18], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33]}
{1: [84, 17, 37, 45, 23, 18], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0]}
{1: [84, 17, 37, 45, 23, 18], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62]}
{1: [84, 17, 37, 45, 23, 18], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61, 82], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13], 5: [3, 1, 28, 52, 40, 91, 61, 82], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7], 5: [3, 1, 28, 52, 40, 91, 61, 82], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41, 99], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71, 71], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41, 99, 15], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71, 71, 42], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41, 99, 15], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71, 71, 42], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41, 99, 15], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3, 36], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
{1: [84, 17, 37, 45, 23, 18, 87, 100, 95, 78, 99, 4, 33], 4: [97, 25, 1, 38, 8, 30, 35, 61, 18, 95, 13, 7, 54, 35, 86, 47, 43, 71, 71, 42], 5: [3, 1, 28, 52, 40, 91, 61, 82, 63, 81, 37, 19, 64, 23, 79, 12, 44, 25, 79, 24, 41, 99, 15, 98], 2: [70, 56, 71, 37, 53, 18, 83, 76, 42, 33, 92, 47, 95, 48, 52, 24, 41, 3, 36], 3: [2, 24, 87, 65, 83, 28, 68, 69, 45, 33, 0, 62, 42, 5, 20, 1, 5, 9, 91, 71, 9, 81, 32, 15]}
print(grade_dic.keys())
dict_keys([1, 4, 5, 2, 3])
grade_cal_dic = {}

for key in grade_dic.keys():
    grade_cal_dic[key] = {}
    grade_cal_dic[key]['min'] = min(grade_dic[key])
    grade_cal_dic[key]['max'] = max(grade_dic[key])
    grade_cal_dic[key]['avg'] = float(sum(grade_dic[key]))/len(grade_dic[key])

print(grade_cal_dic)
{1: {'min': 4, 'max': 100, 'avg': 55.38461538461539}, 4: {'min': 1, 'max': 97, 'avg': 43.85}, 5: {'min': 1, 'max': 99, 'avg': 48.375}, 2: {'min': 3, 'max': 95, 'avg': 51.421052631578945}, 3: {'min': 0, 'max': 91, 'avg': 39.458333333333336}}
# 결과 출력

#grade_list = grade_cal_dic.keys()    #dict_keys([1, 4, 5, 2, 3]) keys()는 리스트가 아니고 dict_keys 객체가 리던된다.
grade_list = list(grade_cal_dic.keys()) #[1, 4, 5, 2, 3]
print(grade_list)

grade_list.sort()
print(grade_list)
[1, 4, 5, 2, 3]
[1, 2, 3, 4, 5]
for key in grade_list:
    print("# grade:", key)
    print("min:", grade_cal_dic[key]['min'], end='')
    print("/ max:", grade_cal_dic[key]['max'], end='')
    print("/ avg:", grade_cal_dic[key]['avg'], end='\n\n')
# grade: 1
min: 4/ max: 100/ avg: 55.38461538461539

# grade: 2
min: 3/ max: 95/ avg: 51.421052631578945

# grade: 3
min: 0/ max: 91/ avg: 39.458333333333336

# grade: 4
min: 1/ max: 97/ avg: 43.85

# grade: 5
min: 1/ max: 99/ avg: 48.375

 

[ 특정앱 (calculate) 의 views.py ]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from django.shortcuts import render, redirect
from django.http import HttpResponse
import pandas as pd
from datetime import datetime
from .models import *
 
def calculate(request):
    file = request.FILES['fileInput']
    # print("# 사용자가 등록한 파일의 이름: ",file)
 
    # 파일 저장하기
    origin_file_name = file.name
    user_name = request.session['user_name']
    now_HMS = datetime.today().strftime('%H%M%S')
    file_upload_name = now_HMS+'_'+user_name+'_'+origin_file_name
    file.name = file_upload_name
    document = Document(user_upload_file = file)
    document.save()
 
    df = pd.read_excel(file,sheet_name='Sheet1',header=0)
    # print(df.head(5))
 
    # grade별 value리스트 만들기
    grade_dic = {}
    total_row_num = len(df.index)
    for i in range(total_row_num):
        data = df.loc[i]
        if not data['grade'in grade_dic.keys():
            grade_dic[data['grade']] = [data['value']]
        else:
            grade_dic[data['grade']].append(data['value'])
 
    # grade별 최소값 최대값 평균값 구하기
    grade_calculate_dic = {}
    for key in grade_dic.keys():
        grade_calculate_dic[key] = {}
        grade_calculate_dic[key]['min'= min(grade_dic[key])
        grade_calculate_dic[key]['max'= max(grade_dic[key])
        grade_calculate_dic[key]['avg'= float(sum(grade_dic[key]))/len(grade_dic[key])
 
    # 결과 출력
    grade_list = list(grade_calculate_dic.keys())
    grade_list.sort()
    for key in grade_list:
        print("# grade:",key)
        print("min:",grade_calculate_dic[key]['min'],end='')
        print("/ max:",grade_calculate_dic[key]['max'],end='')
        print("/ avg:",grade_calculate_dic[key]['avg'],end='\n\n')
    
    # 이메일 주소 도메인별 인원 구하기
    email_domain_dic = {}
    for i in range(total_row_num):
        data = df.loc[i]
        email_domamin = (data['email'].split("@"))[1]
        if not email_domamin in email_domain_dic.keys():
            email_domain_dic[email_domamin] = 1
        else:
            email_domain_dic[email_domamin] += 1
 
    print("## EMAIL 도메인 별 사용인원")
    for key in email_domain_dic.keys():
        print("#",key,": ",email_domain_dic[key],"명")
    # return HttpResponse("calculate, calculate function!")
    
    # grade_calculate_dic 와 email_domain_dic 세션생성
    grade_calculate_dic_to_session = {}
    for key in grade_list:
        grade_calculate_dic_to_session[int(key)] = {}
        grade_calculate_dic_to_session[int(key)]['max'= float(grade_calculate_dic[key]['max'])
        grade_calculate_dic_to_session[int(key)]['avg'= float(grade_calculate_dic[key]['avg'])
        grade_calculate_dic_to_session[int(key)]['min'= float(grade_calculate_dic[key]['min'])
    request.session['grade_calculate_dic'= grade_calculate_dic_to_session
    request.session['email_domain_dic'= email_domain_dic
    return redirect('/result')
cs

 

위에서 42라인 grade_calculate_dic.keys() 은 dict_keys 객체를 반환해 준다.  (파이썬 3.0부터)

(파이썬 2.0에서는 리스트가 반환되었다.)

 

이를 다시 리스트로 바꾸려면

grade_list = list(grade_calculate_dic.keys()) 

 

66라인부터는 

grade_calculate_dic 와  email_domain_dic 라는 두개의 딕셔너리를 세션으로 굽고 

result 함수로 이동하여 결과페이지에 불러와본다.

 

[ 특정앱 (main) > views.py ]

 

1
2
3
4
5
6
7
8
9
10
def result(request):
    if 'user_name' in request.session.keys():
        content = {}
        content['grade_calculate_dic'= request.session['grade_calculate_dic']
        content['email_domain_dic'= request.session['email_domain_dic']
        del request.session['grade_calculate_dic']
        del request.session['email_domain_dic']
        return render(request, 'main/result.html',content)
    else:
        return redirect('main_signin')
cs

 

[ main > templates > main > result.html ]

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
            <div class='body'>
            <div class="resultDiv">
                <h3> * Excel 결과 확인 *</h3>
                <h4> - grade별 최소값, 최대값, 평균값</h4>
                {% for key,value in grade_calculate_dic.items %}
                    <h5>GRADE: {{ key }}</h5>
                    <p><strong>최소값: </strong>{{ value.min }}</p>
                    <p><strong>최대값: </strong>{{ value.max }}</p>
                    <p><strong>평균값: </strong>{{ value.avg }}</p>
                    <br>
                {% endfor %}
                <br>
                <h4> - 이메일별 주소 도메인 인원</h4>
                {% for key,value in email_domain_dic.items %}
                    <p><strong>{{ key }}: </strong>{{ value }}명</p>
                {% endfor %}
                <br>
            </div>
            <hr>
        </div>
cs

 

 

소스 참고 : Django 한그릇뚝딱 (문범우), Chapter 4 중에서