범물1동_강수_202201_202208.xlsx

범물1동_기온_202201_202208.xlsx

import pandas as pd
import numpy as np
import datetime

gion = '범물1동_기온_202201_202208'
gangsu = '범물1동_강수_202201_202208'

filename = './' + gangsu

# csv파일 열기
csvFile = pd.read_csv(filename + '.csv')

day = 1  
month = 1

# 열 이름 선언
column_name = ['date', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

# 저장될 데이터 형식 선언
dataSet = {
    'date': [],
     0: [],  1: [],  2: [],
     3: [],  4: [],  5: [],
     6: [],  7: [],  8: [],
     9: [], 10: [], 11: [],
    12: [], 13: [], 14: [],
    15: [], 16: [], 17: [],
    18: [], 19: [], 20: [], 
    21: [], 22: [], 23: [],
}

# 반복문으로 데이터 서치
for i in range(len(csvFile['hour'])):
    
		# 데이터 형식 맞지 않는 데이터가 나올 경우 넘기기 (예: 'Start : 20220201')
    if csvFile[' format: day'][i] == ' Start : ' + datetime.datetime.strptime(str(1) + "/" + str(month+1) +"/2022", "%d/%m/%Y").strftime('%Y%m%d') + ' ':
        continue
    
		# 해당 일 데이터를 가져와서 비교 -> 다를 경우 다음 달로 넘김
    if day != int(csvFile[' format: day'][i]):
        month += 1
        day = 1
        
    # 해당 일의 시작일 경우 날짜 데이터 넣어주기
    if csvFile['hour'][i] == 0:
        dataSet['date'].append(datetime.datetime.strptime(str(day) + "/" + str(month) +"/2022", "%d/%m/%Y").strftime('%Y-%m-%d'))
       
    # 형식 맞춰서 값 넣어주기 
    dataSet[csvFile['hour'][i] / 100].append(csvFile['value location:90_89 Start : 20220101 '][i])
    
    # 23시일 경우 다음 날로 넘기기
    if csvFile['hour'][i]/100 == 23:
        day += 1 
        
df = pd.DataFrame(dataSet, columns=column_name)
excel_writer = pd.ExcelWriter(filename + '.xlsx', engine='xlsxwriter') # 오류 발생시 `pip install xlsxwriter` 
df.to_excel(excel_writer, index=False, sheet_name=filename)
excel_writer.save()

엑셀파일에 데이터 쓰기 - https://hyunmin1906.tistory.com/100

파이썬 날짜 형식 변경 - https://ko.code-paper.com/php/examples-python-change-date-format-to-dd-mm-yyyy

xlsxwriter 오류 해결 - https://joyfulhome.tistory.com/215