티스토리 뷰

euc-kr 인코딩으로 한글을 문제없이 쓸수 있다.


decorator 로 예외 처리를 하였다.




def handler(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
return wrapper


@handler
def write(path, data, encoding='euc-kr'):
with open(path, 'w', encoding=encoding) as f:
f.writelines("\n".join([",".join(list(map(str, row))) for row in data]))


@handler
def read(path, encoding='euc-kr'):
with open(path, 'r', encoding=encoding) as f:
return [[item if '\n' not in item else item.replace('\n', '') for item in line.split(',')]for line in f.readlines()]


댓글