본문 바로가기

Python

Python - 특정 문자가(정규식이) 문자열에 포함되어 있는지 검사 (java의 String.contains) python에서 정규식 관련 매칭, 검색기능을 제공하는 re 모듈을 사용한다. re.compile(패턴) 함수로 Pattern 오브젝트를 생성 한 후 Pattern.search(문자열) 함수를 이용하여 결과를 확인한다. search 함수는 Pattern 오브젝트의 패턴이 문자열에 존재하면 Match 오브젝트를 그렇지 않으면 None을 리턴하므로 아래와 같이 활용하여 특정 문자나, 정규식을 검색 할 수 있다. import re string_a = 'test ㅋ' string_b = '1test' digit_regexp = re.compile(r'\d') space_regexp = re.compile(r'ㅋ') if digit_regexp.search(string_a): print('{0} has digit.. 더보기
Python - Beautiful Soup 사용법 html 태그 문자열로 BeautifulSoup 객체를 생성한다. from bs4 import BeautifulSoup soup = BeautifulSoup(html_string, 'html.parser') CSS selector 기준으로 tag를 찾아 리스트 형태로 반환한다. (CSS Selector : https://www.w3schools.com/cssref/css_selectors.asp) # soup.select(CSS selector) soup.select('select#observation_select1 option') # HTML Tag - 서울(유) print(location_selector) # 서울(유) - HTML 문자열 print('selected' in location_selec.. 더보기
Python - 이전 달 구하기 python 에서 이전 달을 구하는 방법은 현재 달의 1일 - 1일 하는 형태로 이전 달을 구할 수 있다. from datetime import date, timedelta today = date.today() month_ago = today.replace(day = 1) - timedelta(days = 1) year = str(month_ago.year) month = str(month_ago.month) 더보기
Python - String 함수 참고 : https://docs.python.org/3/library/stdtypes.html#string-methods str.capitalize()첫 글자만 대문자로 바꾸고 나머지는 소문자로 변경 print("capitalize() : ", "abcabcAbCd".capitalize()) # capitalize() : Abcabcabcd str.casefold()문자열을 모두 Unicode로 변환하고 소문자로 변경 print("casefold() : ", "abcAbCdßß".casefold()) # casefold() : abcabcdssss str.center(width [, fillchar])width 의 길이를 가지는 문자열로 변환, 원래 문자열보다 width 가 작으면 원래 문자열 반환.원래.. 더보기
Python 어렵게 배우기 - Exercise 41: Learning To Speak Object Oriented 원문 : http://learnpythonthehardway.org/book/ex41.html 객체 지향에 대해 설명하는 장 으로 어설프게 번역하느니 원문을 직접 보시고, 객체 지향 프로그래밍(OOP)이 무엇인지 더 알고 싶다면 다른 자료들을 더 찾아보길 권함. 더보기
Python GUI Frameworks wxPythonPython GUI Frameworkwxformbuilder를 사용하면 폼 구성을 좀 쉽고 편하게 할 수 있다.tutorialhttp://www.zetcode.com/wxpython/ KIVYCrossplatform UI FrameworkWindows, OS X, Android, iOS 와 같은 다양한 OS에 동작하는 멀티 터치 GUI 프로그래밍을 할 수 있다.http://kivy.org/ Windows, OSX, Android, iOS 를 지원한다. 더보기
Python 어렵게 배우기 - Exercise 40: Modules, Classes, and Objects 원문 : http://learnpythonthehardway.org/book/ex40.html 개발을 하다보면 다른사람이 만든 module 이나 class를 가져다 쓸 경우가 있다.import 라는 명령어를 사용해서 외부 라이브러리(다른사람들이 만든 프로그램)를 가져다 쓸 수 있다. Modulepython 의 함수, 변수, class 와 같은 코드를 담고 있는 ".py" 파일 하나를 하나의 모듈 이라고 생각하면 된다. 아래와 같은 모듈이 있다고 했을 때 (mystuff.py) def apple(): print "I AM Apples!" tangerine = "Living reflection of a dream" 위 코드를 사용하고 싶으면 (python shell)... import mystuff myst.. 더보기
Python - Custom Exception (사용자 정의 Exception) #Exception 클래스를 상속한 클래스를 만든다 class CustomException(Exception): #생성할때 value 값을 입력 받는다. def __init__(self, value): self.value = value #생성할때 받은 value 값을 확인 한다. def __str__(self): return self.value #예외를 발생하는 함수 def raise_exception(err_msg): raise CustomException(err_msg) #테스트 해보자 try: raise_exception("Error!!! Error~!!!") except CustomException, e: print e #Error!!! Error~!!! 더보기