[Python] 22. 표준 라이브러리(Standard Library)(2)

2023. 11. 7. 22:06·Programming Language(Sub)/Python
목차
  1. fnmatch
  2. csv
  3. Python Standard Library, 그 이후
  4. tqdm

표준 라이브러리(standard library)(2)


fnmatch


  • Unix-sytle Pattern Matching

  • 유닉스 shell에서 사용되는 rule에 따라 특정 패턴과 일치하는 string을 찾음

  • fnmatch(text, pattern)

    • text가 pattern과 match하는지 Test (T/F)

    • 대소문자 구분하지 않음(case-insensitive)

  • fnmatchcase(text, pattern)

    • fnmatch와 유사하나, 대소문자를 구분함
  • filter(iterable, pattern)

    • 패턴을 만족하는 iterable에 있는 요소들의 list를 반환
  • example

    import fnmatch
    descs = ['My name is Lee and my E-mail is kim@google.com.', 
    'My name is Park and my e-mail address is Park@naver.com.']
    
    print([fnmatch.fnmatch(desc, 'e-mail') for desc in descs]) # [False, False]
    print([fnmatch.fnmatch(desc, '*e-mail*') for desc in descs]) # [True, True]
    print([fnmatch.fnmatchcase(desc, '*e-mail*') for desc in descs]) # [False, True]
    print([fnmatch.fnmatchcase(desc, '*[Ee]-mail*') for desc in descs]) # [True, True]
    
    print(fnmatch.filter(descs, '*e-mail*')) # ['My ... Lee', 'My ... Park']
    print(fnmatch.filter(descs, '*Pa?k*')) # ['My ... Park']
  • [Ee]는 E와 e 중 한 개의 문자와 매치함을 뜻하는데, 이 표현은 정규식 표현 중 한가지임


csv

  • CSV format(comma-separted values)에 표로 나타낸 데이터를 읽고 쓰는 class를 담고있다.

  • reader(file_obj, dialect='exce', **fmtparams)

    • file_obj의 행에 접근할 수 있는 reader object를 return
  • writer(file_obj, dialect='exce', **fmtparams)

    • user data를 file_obj에 컴마(,)로 구분된 문자열로 변환하여 쓰는 writer object 반환
  • example

    import glob, csv
    
    files = glob.glob('workspace/data_??.csv')
    all_data = []
    for file in files:
        with open(file, 'r') as f:
            csv_reader = csv.reader(f)
            data = []
            for line in csv_reader: # ex) line = ['11', '45']
                if line and not line[0].strip().startswith('#'):
                    data.append([int(val) for val in line])
            all_data = all_data + data

Python Standard Library, 그 이후


  • pre-built Python library를 설치해서 사용할 수 있음

  • pip install package_name

    (pip3 install package name) --> mac OS

    • ex) pip3 install opencv-python

    • pip3 install numpy

tqdm

  • 콘솔에 iteration의 진행상황을 시각화 해준다

  • example

from tqdm import tqdm
n = 1000000000
for i in tqdm(range(n)):
    pass

참조) Do it! 점프 투 파이썬 (박응용 지음), https://wikidocs.net (파이썬 계단밟기)

'Programming Language(Sub) > Python' 카테고리의 다른 글

[Python] 24. GUI(Graphic User Interface) - tkinter  (0) 2023.11.08
[Python] 23. 정규 표현식(regular expression)  (0) 2023.11.07
[Python] 21. 표준 라이브러리(Standard Library)(1)  (0) 2023.11.07
[Python] 20. 내장 함수(Built-in-function)  (0) 2023.11.07
[Python] 19. 예외처리(Exception Handling)  (0) 2023.11.07
  1. fnmatch
  2. csv
  3. Python Standard Library, 그 이후
  4. tqdm
'Programming Language(Sub)/Python' 카테고리의 다른 글
  • [Python] 24. GUI(Graphic User Interface) - tkinter
  • [Python] 23. 정규 표현식(regular expression)
  • [Python] 21. 표준 라이브러리(Standard Library)(1)
  • [Python] 20. 내장 함수(Built-in-function)
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기 (452) N
      • Software Development (27)
        • Performance (0)
        • TroubleShooting (1)
        • Refactoring (0)
        • Test (8)
        • Code Style, Convetion (0)
        • DDD (0)
        • Software Engineering (18)
      • Java (71)
        • Basic (5)
        • Core (21)
        • Collection (7)
        • 멀티스레드&동시성 (13)
        • IO, Network (8)
        • Reflection, Annotation (3)
        • Modern Java(8~) (12)
        • JVM (2)
      • Spring (53) N
        • Framework (12)
        • MVC (23)
        • Transaction (3) N
        • AOP (11) N
        • Boot (0)
        • AI (0)
      • DB Access (1)
        • Jdbc (1)
        • JdbcTemplate (0)
        • JPA (14)
        • Spring Data JPA (0)
        • QueryDSL (0)
      • Computer Science (125)
        • Data Structure (27)
        • OS (14)
        • Database (10)
        • Network (21)
        • 컴퓨터구조 (1)
        • 시스템 프로그래밍 (23)
        • Algorithm (29)
      • HTTP (8)
      • Infra (1)
        • Docker (1)
      • 프로그래밍언어론 (15)
      • Programming Language(Sub) (77)
        • Kotlin (1)
        • Python (25)
        • C++ (51)
        • JavaScript (0)
      • FE (11)
        • HTML (1)
        • CSS (9)
        • React (0)
        • Application (1)
      • Unix_Linux (0)
        • Common (0)
      • PS (13)
        • BOJ (7)
        • Tip (3)
        • 프로그래머스 (0)
        • CodeForce (0)
      • Book Review (4)
        • Clean Code (4)
      • Math (3)
        • Linear Algebra (3)
      • AI (7)
        • DL (0)
        • ML (0)
        • DA (0)
        • Concepts (7)
      • 프리코스 (4)
      • Project Review (6)
      • LegacyPosts (11)
      • 모니터 (0)
      • Diary (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
lumana
[Python] 22. 표준 라이브러리(Standard Library)(2)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.