# 자주 쓸 것 같은 Python Standard libraries 메모



# Python Standard library: https://docs.python.org/3/library/



# 강의에서 추천


  • csv: very convenient for reading and writing csv files
  • collections: useful extensions of the usual data types including OrderedDictdefaultdict and namedtuple
  • random: generates pseudo-random numbers, shuffles sequences randomly and chooses random items
  • string: more functions on strings. This module also contains useful collections of letters like string.digits (a string containing all characters with are valid digits).
  • re: pattern-matching in strings via regular expressions
  • math: some standard mathematical functions
  • os: interacting with operating systems
  • os.path: submodule of os for manipulating path names
  • sys: work directly with the Python interpreter
  • json: good for reading and writing json files (good for web work)




# 상황별

1. 날짜와 시간 관련: datetime

   - https://docs.python.org/3/library/datetime.html



2. 현재의 working directory 바꾸기: os

   os.chdir(path)

   - https://docs.python.org/3/library/os.html



3. csv파일 읽기: csv

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

  - https://docs.python.org/3/library/csv.html



4. zip 파일 압축해제: zipfile

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())

   - https://docs.python.org/3/library/zipfile.html



5. 실행시간 측정: cProfile

   - https://docs.python.org/3/library/profile.html

   - 작은 수행일때는 timeit도 많이 쓴다.

      https://docs.python.org/3/library/timeit.html



Reference:

- https://classroom.udacity.com/courses/ud1110

- https://docs.python.org/3/library/

+ Recent posts