from openpyxl import *
''' openpyxl은 엑셀을 불러오는 모듈이다. '''

In [2]:

wb = load_workbook('test.xlsx')
wb.sheetnames

Out[2]:

['Sheet1']

In [3]:

ws = wb.active

In [4]:

ws

Out[4]:

<Worksheet "Sheet1">

In [5]:

g = ws.rows
print(g)
cells = next(g)
cells
<generator object Worksheet._cells_by_row at 0x7fb829888bf8>

Out[5]:

(<Cell 'Sheet1'.A1>,
 <Cell 'Sheet1'.B1>,
 <Cell 'Sheet1'.C1>,
 <Cell 'Sheet1'.D1>)

In [6]:

keys= []
for cell in cells:
    keys.append(cell.value)
keys

Out[6]:

['name', 'math', 'literature', 'science']

In [7]:

student_data = []
for row in g:
    dic = {k : c.value for k, c in zip(keys, row)}
    student_data.append(dic)
student_data

Out[7]:

[{'name': 'gr', 'math': 95, 'literature': 65, 'science': 75},
 {'name': 'jo', 'math': 25, 'literature': 30, 'science': 55},
 {'name': 'ya', 'math': 50, 'literature': 45, 'science': 40},
 {'name': 'ti', 'math': 15, 'literature': 65, 'science': 90},
 {'name': 'me', 'math': 100, 'literature': 100, 'science': 100},
 {'name': 'th', 'math': 10, 'literature': 15, 'science': 20},
 {'name': 'el', 'math': 25, 'literature': 50, 'science': 100},
 {'name': 'ma', 'math': 79, 'literature': 75, 'science': 80},
 {'name': 'st', 'math': 95, 'literature': 100, 'science': 95},
 {'name': 'an', 'math': 20, 'literature': 20, 'science': 20}]

In [8]:

import math
import openpyxl

In [9]:

#용어정리
#평균 (average)
#산포도 (dispersion)
#분산 (variance)
#표준편차 (standard deviation)

raw_data = {}
wb = openpyxl.load_workbook('test2.xlsx')
ws = wb.active
g = ws.rows
for name, score in g:
    raw_data[name.value] = score.value
# print(raw_data)
scores = list(raw_data.values())
# print(scores)

s = 0

for score in scores:
    s += score
    
avrg = round(s/len(scores), 1)
# print(avrg)

s = 0

for score in scores:
    s += (score - avrg) ** 2

    variance = round(s/len(scores), 1)
    
    std_dev = round(math.sqrt(variance),1)
    
print(
    "평균: {}, 분산: {}, 표준편차: {}".format(avrg,variance,std_dev)
)
if avrg <50 and std_dev >20:
    print("성적이 너무 저조하고 학생들의 실력 차이가 너무 크다.")
elif avrg > 50 and std_dev >20:
    print("성적은 평균 이상이지만 학생들이 실력 차이가 크다. 주의 요망!")
elif avrg < 50 and std_dev <20:
    print("학생들의 실력 차이는 크지 않지만 성적이 너무 저조하다. 주의 요망!")
elif avrg > 50 and std_dev <20:
    print("성적도 평균 이상이고 학생들의 실력 차이도 크지 않다.")
평균: 51.4, 분산: 1234.6, 표준편차: 35.1
성적은 평균 이상이지만 학생들이 실력 차이가 크다. 주의 요망!

In [10]:

#function.py; 각 기능을 함수화

import openpyxl
import math

def get_data_from_excel(filename):
    '''
    엑셀 파일에서 데이터를 가져와 dic형태로 저장
    '''
    dic = {}
    wb = openpyxl.load_workbook(filename)
    ws = wb.active
    g = ws.rows

    for name, score in g:
        dic[name.value] = score.value

    return dic

def average(scores):
    s = 0
    for score in scores:
        s += score
    return round(s/len(scores), 1)

def variance(scores, avrg):
    s= 0
    for score in scores:
        s += (score - avrg) ** 2
    return round(s/len(scores), 1)

def std_dev(variance):
    return round(math.sqrt(variance),1)

def evaluateClass(avrg, total_avrg, std_dev, sd):
    '''
    evaluateClass(avrg, total_avrg, std_dev, sd) -> None
    avrg: 반 성적 평균
    total_avrg: 학년 전체 성적 평균
    std_dev: 반의 표준편차
    sd: 원하는 표준편차 기준
    '''

    if avrg <total_avrg and std_dev >sd:
        print("성적이 너무 저조하고 학생들의 실력 차이가 너무 크다.")
    elif avrg > total_avrg and std_dev >sd:
        print("성적은 평균 이상이지만 학생들의 실력 차이가 크다. 주의 요망!")
    elif avrg < total_avrg and std_dev <sd:
        print("학생들의 실력 차이는 크지 않지만 성적이 너무 저조하다. 주의 요망!")
    elif avrg > total_avrg and std_dev <sd:
        print("성적도 평균 이상이고 학생들의 실력 차이도 크지 않다.")

In [11]:

#main.py; 함수화된 기능을 사용하는 main코드

#from functions import *; notebook에서는 불러왔다는 가정을하고 작성한다.

# if __name__ = = "__main__": ; 이부분 역시 필요없다.

raw_data = get_data_from_excel('test2.xlsx')
scores = list(raw_data.values())

avrg = average(scores)
variance = variance(scores, avrg)
standard_deviation = std_dev(variance)

print("평균: {}, 분산: {}, 표준편차: {}".format(
    avrg, variance, standard_deviation))
evaluateClass(avrg, 50, standard_deviation, 20)
평균: 51.4, 분산: 1234.6, 표준편차: 35.1
성적은 평균 이상이지만 학생들의 실력 차이가 크다. 주의 요망!