---
# System prepended metadata

title: 處理資料程式碼 - 正規化
tags: [期末專題, 計算智慧與規劃]

---

###### tags: `計算智慧與規劃` `期末專題`
# 處理資料程式碼 - 正規化
```python=
import csv
def pick(data) :
    newdata = []
    for i in range(len(data)) :
        if (data[i][2] == "PM2.5               " or data[i][2] == "AMB_TEMP            ") :
             newdata.append(data[i])
        if (data[i][2] == "CO                  " or data[i][2] == "NO                  ") :
            newdata.append(data[i])
        if (data[i][2] == "NO2                 " or data[i][2] == "NOx                 ") :
            newdata.append(data[i])
        if (data[i][2] == "O3                  " or data[i][2] == "PM10                ") :
            newdata.append(data[i])
        if (data[i][2] == "RAINFALL            " or data[i][2] == "RH                  ") :
            newdata.append(data[i])
        if (data[i][2] == "SO2                 ") :
            newdata.append(data[i])
    return newdata
def changeType(data, name) :
    #averageList = []
    for i in range(len(data)) :
        data[i][0] = name
        num = 0
        for j in range(3,len(data[i])) :
            try :
                data[i][j] = float(data[i][j])
            except :
                data[i][j] = "empty"
    #url = 'output'+name+'_2018.csv'
    #with open(url, 'w', newline='') as csvfile:
    #    writer = csv.writer(csvfile, delimiter=',')
    #    writer.writerows(data)

def check(data) :
    newdata = []
    for i in range(len(data)) :
        num = 0
        for j in range(len(data[i])) :
            if data[i][j] != "empty" :
                num = num + 1
        if (num == 12) :
            newdata.append(data[i])
    return newdata
def turn(data) :
    # 12 項分別是10個變數、pM2.5、時間
    newdata = [["empty" for i in range(12)] for i in range(366*24)]
    num = 0
    print(data[0:24])
    for i in range(366) :
        for pos in range(24) :
            try :
                newdata[i*24+pos][0] = data[i*11][1] + str(pos)
                for v in range(11) :
                    newdata[i*24+pos][v+1] = data[i*11+v][pos+3]
                
            except :
                newdata[i*24+pos][0] = "empty"
    return newdata
    
def changeSpace(data) :
    pos = len(data[0])
    for i in range(len(data)) :
        tmp = data[i][pos-1]
        data[i][pos-1] = data[i][pos-4]
        data[i][pos-4] = tmp
    #print(data[len(data)-1],data[len(data)-2])
    
def aver(data) :
    MinList = [1000 for i in range(11)]
    MaxList = [0 for i in range(11)]
    print(data[0])
    for i in range(len(data)) :
        for pos in range(11) :
            if (MaxList[pos] < data[i][pos+1]) :
                MaxList[pos] = data[i][pos+1]
            if (MinList[pos] > data[i][pos+1]) :
                MinList[pos] = data[i][pos+1]
    print("MinList", MinList, "MaxList", MaxList)
    for i in range(len(data)) :
        for pos in range(11) :
            data[i][pos+1] = (data[i][pos+1] - MinList[pos]) / (MaxList[pos] - MinList[pos])
    
def main() :
    data = []
    name = input()
    #url = "C:/Users/rita5/Desktop/鋼鐵/全部_2019/高屏空品區_2019/高屏空品區/"+name+'_2019.csv'
    url = "C:/Users/rita5/Desktop/鋼鐵/全部_2020/"+name+'_2020.csv'
    with open(url, newline= '') as csvfile :
        rows = csv.reader(csvfile, delimiter = ',')
        for row in rows :
            data.append(row)
    #print(data[2])
    data = pick(data)
    #print(data[0])
    changeType(data, name)
    data = turn(data)
    data = check(data)
    changeSpace(data)
    # 正規化
    aver(data)
    print(data)
    url = 'Normailization'+name+'_2020.csv'
    with open(url, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerows(data)

if __name__ == "__main__" :
    main()
```