티스토리 뷰

SWDesk/App

[Python] 파일에서 데이터 읽기

bizmaker 2020. 10. 25. 23:02

1. 파일에서 데이터를 읽어서 Dict 형태로 불러오기

 

2. 전처리를 위해 빈 데이터 채우기

<추후 계획>

3. 특정 항목별로 값 배열 만들기(class, array 활용)

4. 특정 범위(일정 기간 등) 값에 대한 최대값, 최소값, 평균, 표준편차 등을 계산

 

def Test14(weatherStation1):
    filename = "E:/tttt/"+weatherStation1['StationCode']+"_AllData01.csv"
    file1 = open(filename, 'r')
    keyLine = file1.readline()
    keys1 = keyLine.split(",")
    keyNumber = len(keys1)
    dataList = []
    while(True):
        valueLine = file1.readline()
        if not valueLine: break
        values1 = valueLine.split(",")	
        values_dict = {}
        for index1 in range(keyNumber-1):
            values_dict[keys1[index1]] = values1[index1]
        dataList.append(values_dict)
    print(len(dataList))
    print(dataList[1])
    file1.close()
    return dataList, keys1

def Test151(Time0, Time1): # To find day difference
    if Time0 == Time1: return 0
    if Time0 > Time1: return -1
    times0 = Time0.split("-")
    year0 = int(times0[0])
    month0 = int(times0[1])
    day0 = int(times0[2])
    times1 = Time1.split("-")
    year1 = int(times1[0])
    month1 = int(times1[1])
    day1 = int(times1[2])
    datetime0 = datetime.datetime(year0, month0, day0)
    datetime1 = datetime.datetime(year1, month1, day1)
    datetime01 = datetime0 + timedelta(days=1)
    dayDifference = 1
    while(True):
        if(datetime01 == datetime1): return dayDifference
        datetime01 = datetime01 + timedelta(days=1)
        dayDifference += 1

    return -9

def Test152(keys, data0, data1, dayDifference, repeatNo): # To fill empty data

    canBeAdded = False
    newData = {}
    keyNumber = len(keys)
    for index11 in range(keyNumber-1):
        if canBeAdded:
            try:
                value0 = float(data0[keys[index11]])
                value1 = float(data1[keys[index11]])
                valueD = (value1 - value0)
                newValue = repeatNo*valueD/(dayDifference+1)
                newData[keys[index11]] = newValue
            except:
                print(data0)
                return
        else:
            if keys[index11] == "tm":
                times0 = data0[keys[index11]].split("-")
                year0 = int(times0[0])
                month0 = int(times0[1])
                day0 = int(times0[2])
                time0 = datetime.datetime(year0, month0, day0)
                datetime01 = time0 + timedelta(days=repeatNo)
                newData[keys[index11]] = datetime01.strftime('%Y-%m-%d')
                canBeAdded = True
            else:
                newData[keys[index11]] = data0[keys[index11]]
    return newData



def Test15(weatherStation1): #Preprocessing
    dataList, keys0 = Test14(weatherStation1)
    dataList2 = sorted(dataList, reverse=False, key=(lambda x:x['tm']))
    index1=0
    dataNumber = len(dataList2)
    print("DataNumber1:", dataNumber)
    for data1 in dataList2:
        data2 = dataList2[index1+1]
        dayDifference1 = Test151(data1['tm'], data2['tm'])
        if(dayDifference1 == 0):                                 
            del dataList[index1]
        elif dayDifference1 < 0:
            print("Reversed date")
            return
        elif dayDifference1 > 1:
            repeatNo = 1
            print(data1, data2)
            while(True):
                addedData = Test152(keys0, data1, data2, dayDifference1, repeatNo)
                dataList2.insert(index1+repeatNo, addedData)
                print("[Added]")
                print(dataList2[index1+repeatNo])
                repeatNo+=1
                if(repeatNo > dayDifference1): break
        index1+=1
        if index1 >= (dataNumber-1): break
    dataNumber = len(dataList2)
    print("DataNumber1:", dataNumber)
    return dataList2

WeatherStationList = LoadWeatherStations()
#SeaWeatherStationList = LoadSeaWeatherStations()
theBeginning = datetime.datetime(2010, 1, 1)
theNow = datetime.datetime.today()

weatherStation1 = WeatherStationList[0]		
DataList1 = Test15(weatherStation1)
반응형