Python(6)
-
Python @classmethod, @staticmethod 차이점
차이점은 부모 클래스에게서 상속 받을때 나타난다. 1. classmethod는 해당cls를 받아 값이 변경이되어 사용하고 2. staticmethod는 부모의 속성을 값을 그대로 받아 사용한다. 예시 class checkMethod(object): checkWord = '안녕하세요!' def __init__(self): self.greetings = '파이썬!' + self.checkWord @classmethod def classMode(cls): return cls() @staticmethod def staticMode(): return checkMethod() def printWord(self): print(self.greetings) class answer(checkMethod): checkWor..
2021.07.10 -
python 해당 값 찾기 in 연산자
data = ['짱구', '철수', '훈이', '유리', '맹구'] if '짱구' in data: print('짱구있어요!') if '신형만' in data: print('신형만 있어요!') elif '신형만' not in data: print('신형만 없어요!') #짱구있어요! #신형만 없어요! 해당값이 있는지 in 연사자를 이용해 확인이 가능하고, 값이 없는지 확인은 not 연산자를 이용해 확인 할 수 있다.
2021.07.09 -
string 자료형 문장부호 없애는 방법 string.puctuation
string 자료형에서 ' , . ! ? 등 문장을 읽기 쉽게 나타낼 때 import string data ='Let`s go to lunch!' result = data.translate(str.maketrans('','',string.punctuation)) print(result) # Lets go to lunch # 다른방법 punct = string.punctuation for c in punct: data = data.replace(c, "") print(data) # Lets go to lunch 출처: https://www.delftstack.com/ko/howto/python/how-to-strip-punctuation-from-a-string-in-python/#%25ED%258C%25..
2021.07.09 -
cp949' codec can't decode byte 0xec in position 84: illegal multibyte sequence 오류 해결 json파일 가져올때
json파일을 가져오는 중에 cp949' codec can't decode byte 0xec in position 84: illegal multibyte sequence 이란 오류가 발생했다 open('./도서.json') as file: realData = json.load(file) # 'rt', encoding='UTF8'를 추가해서 실행하면 된다. open('./도서.json', 'rt', encoding='UTF8') as file: realData = json.load(file) cp949 코덱으로 인코딩 된 파일을 읽을때 문제가 생긴다고 한다. 출처 : https://airpage.org/xe/language_data/20205
2021.06.30 -
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 14: invalid continuation byte 해결 방법
encoding 부분을 'ISO-8859-1'로 변경해주면 파일을 읽을 수 있다. encoding='utf-8' => encoding='ISO-8859-1'
2021.05.14 -
SyntaxError: Non-ASCII character 에러 해결
파이썬을 실행할때 python 과 python3 명령어가 있다. python 명령어는 인코딩이 ASCLL이고 python3 명령어는 인코딩이 UTF-8이다 그래서 python 명령어를 사용 할 때 는 가장 첫 줄에 # -*- coding: utf-8 -*- 라고 적어 주고 실행해야 한다. 하지만 python3명령어는 utf-8로 인코딩을 하기때문에 정상 작동한다. 출처: https://korbillgates.tistory.com/97
2021.05.14