파이썬(49)
-
__init__.py
깃허브 같은 곳에서 Python 모듈을 보면 "__init__.py"라는 파일을 보게 된다. "__init__.py"은 package initialization file(패키지 초기화 파일)이라고 부르며 이 파일이 있다는 것은 Python system에 해당 디렉토리가 패키지라는 것을 의미한다. 반대로 생각하면 "__init__.py"가 없다면 Python은 해당 디렉터리의 모듈을 가져오지 못한다. module 디렉토리에 "__init__.py"가 없는 경우 Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "li..
2017.07.24 -
파이썬을 이용한 액셀파일(xls, xlsx) 처리 - openpyxl
액셀파일(xls, xlsx)을 읽어 데이터를 추출해야 하는 경우가 발생하였다.당연히 액셀파일을 컨트롤 할 수 있는 라이브러리가 존재한다. 홈페이지: https://openpyxl.readthedocs.org/en/latest/ #!/usr/bin/pythonimport openpyxl wb = openpyxl.load_workbook('XXXXXX.xlsx', data_only=True)ws = wb.activeprint ws['C5'].value + ' = ' + ws['G5'].value 원하는 셀(Cell)의 데이터를 읽고 처리할 수 있다.다만 특정 셀의 데이터가 수식인 경우 값이 아니라 수식이 반환되는데 이 경우에는 load_workbook()으로 로딩할때 data_only 파라미터를 True로 ..
2015.09.15 -
String methods - str.strip([chars])
https://docs.python.org/2/library/stdtypes.html?highlight=str.strip#str.strip str.strip([chars])문자열 양 끝에 있는 공백을 없애야 하는 경우에 사용을 한다.파라미터로 character를 받는데 공백이 아니라 특정 문자를 제거하고 싶을 경우에 사용하면 된다. >>> ' xxx x'.strip()'xxx x'>>> ' xxx x'.strip(' ')'xxx x'>>> ' xxx x'.strip('x')' xxx '>>> ' xxx x x'.strip('x')' xxx x '>>> 'x xxx x x'.strip('x')' xxx x '>>> ' x xxx x x'.strip('x')' x xxx x '>>> ' x xxx x x '..
2015.09.15 -
내장함수 - long(x, base=10)
https://docs.python.org/2/library/functions.html#long class long(x=0)class long(x, base=10)long integer class를 리턴한다.Param이 없는 경우는 0L를 리턴한다. >>> print long() 0 >>> print long(10) 10 >>> print long('10')10>>> print long('10', 10) 10 >>> print long('10', 16) 16 >>> print long('16', 8) 14 >>> print long('16.0', 10) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal fo..
2015.07.26 -
내장함수 - locals()
https://docs.python.org/2/library/functions.html#locals locals()현재 namespace를 딕셔너리로 구성하여 리턴한다.globals() always returns the dictionary of the module namespacelocals() always returns a dictionary of the current namespace >>> def test(): ... a = 1 ... b = 2 ... huh = locals() ... print(huh) ... >>> test() {'a': 1, 'b': 2} >>> locals() {'__builtins__': , '__name__': '__main__', 'test': , '__doc__': ..
2015.07.25 -
내장함수 - list([iterable])
https://docs.python.org/2/library/functions.html#list class list([iterable])Param을 list로 구성하여 리턴한다. >>> list([1, 2, 3]) [1, 2, 3] >>> list((1, 2, 3))[1, 2, 3]>>> list('LIST') ['L', 'I', 'S', 'T'] >>> list() [] >>>
2015.07.25