반응형
if 1 == 1:
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
import os
import pandas as pd
class OrderFile:
def __init__(self, filepath):
self.filepath = filepath
self.initData()
def get_excel_data_type(self):
return {
"열1": str,
"열2": str,
"열3": str,
}
def initData(self):
self.filepath = os.path.normpath(self.filepath)
self.filename = os.path.basename(self.filepath)
columns = self.get_excel_data_type()
try:
self.df_order = pd.read_excel(self.filepath, converters=columns, keep_default_na="")
self.df_order = self.df_order.loc[:, list(columns.keys())]
except Exception as e:
print(e)
def output(self):
print(self.df_order)
if __name__ == "__main__":
order_file = OrderFile(r"D:\excels\엑셀.xlsx")
order_file.output()
열의 이름을 해당 클래스에서 미리 입력받아 놓으면 추후 유지보수에 편리하다.
예) 엑셀에 열을 추가해서 다른 기능을 추가하고 싶다... 등
반응형
'프로그래밍 > python' 카테고리의 다른 글
APScheduler 특정 시간에 함수 실행을 예약하고 예약을 취소하는 방법 (0) | 2023.01.17 |
---|---|
for문에서 각각 변수명을 선언하는 방법 (0) | 2023.01.16 |
비동기 처리 (0) | 2023.01.12 |
string이 특정 html 태그 안에 있는지 확인해보는 정규식 (0) | 2023.01.12 |
json 파일 다루기 (0) | 2023.01.11 |