3 분 소요

File & Exception & Log handling

exception

-> Exception handling : 예외 처리

try ~ except 문법

try:
  예외 발생 가능 코드
except <Except Type>:
  예외 발생시 발생 코드

raise 구문

raise <Exception Type>(예외정보)

assert 구문

assert 예외조건

File Handling

기본적인 파일 : text 파일, binary파일

text file handling

접근모드

r (읽기), w (쓰기), a (추가)

rb (binary 읽), wb, ab

‘r’ mode

#f에 파일 주소 입력
f = open("<파일이름>", '접근모드')
# content 에 파일 내용 입력
contents = f.read()
f.close()

with 구문과 같이 사용하기

with open("mydream.txt", "r") as my_file:
  contents = my_file.read()
  print(type(contents), contents)

read() : 통채로 읽음

readlines() : 한줄씩 전체로 읽어 옴

readline() : 실행시마다 한줄 씩 읽음

‘w’ mode

 f = open("cont.txt",'w',encoding = 'utf8')
 f.write("hi,what")
 f.close

window 는 cp949 / linux 에서는 utf8 사용

with open("count.txt", 'w', encoding = 'utf8') as f:
  f.write("hi,what")

파이썬의 directory 다루기

import os
# log 폴더 생성
os.mkdir("log")

폴더 있는지 확인

os.path.exists("log")
os.path.isfile("file.ipynb")
import shutil

source = "mydream.txt"
dest = os.path.join("abc","sungchul.txt")

#파일 복사 함수
shutil.copy(source,dest)

최근에는 pathlib 모듈을 사용하여 path를 객체로 다룸

import pathlib

cwd = pathlib.Path.cwd()

cwd #현재
cwd.parent #부모
cwd.parent.parent #조부모


Log 파일 생성하기

현재까지 진행되는 방향 볼 수 있는 파일

-> 방법이 너무 많아서 구글링하라..

Pickle

파이썬의 객체를 영속화 하는 built in 객체

(따로 pickle 파일을 만들어 영속화 해 놓았음)

import pickle

	

Logging handling

기본 log 관리 모듈 : logging

import logging

logging.debug("shit")

debug > info > warning > error > critical

기본적인 로깅 레벨은 warning 부터 시작함

import logging

if __name__ == '__main__':
	logger = logging.getLogger("main")
	logging.basicConfig(level = logging.DEBUG) #기본적으로 debug 부터 시작해라
	logger.setLevel(logging.ERROR) #출력은 error 부터 시작해라

	steam_handler = logging.FileHandler(
				"my.log", mode = "a", encoding = "utf8")
	logger.addHandler(steam_handler)
  #my.log 라는 파일에 logging 출력하겠다
	#mode = ‘r’ : 파일 읽기
	#mode = ‘w’ : 파일 쓰기
	#mode = ‘a’ : 파일 append(추가 작성)

	logger.debug("1.")
	logger.info("2.")
	logger.warning("3.")
	logger.error("4.")
	logger.critical("5."	)

Log 파일 세팅 해주는법 2가지

1.configparser - 파일에

프로그램의 실행 설정을 file에 저장

section, key, value 형태 설정된 파일 사용

설정 파일을 dict type으로 호출 후 사용

2. argparser - 실행시점에

console 창에서 프로그램 실행시 setting 정보 저장

command line option 이라고 부름

일반적으로 argparse 사용

Python data handling

CSV

comma separate value

csv 객체로 csv 처리

import csv


with open("abc.csv", "r", encoding = "cp949") as p_file:
  csv_data = csv.reader(p_file) #csv객체 이용해서 csv파일 읽기
  for row in csv_data: # 읽어온 데이터를 한줄씩 처리


with open("bcd", 'w' , encoding = "utf8") as s_p_file:
  

Web

www : world wide web

인터넷 공간의 정식 명칭

데이터를 대부분 여기서 가저온다

HTML

-구조

<html> - <head> 

				-<body> 

regular expression

정규식, regexp, regex로 불림

복잡한 무자열 패턴을 정의하는 문자 표현 공식

문법 1) 문자 클래스 [ ]

[ ] 안의 문자들과 매치

”-“ 를 사용하여 범위를 지정할 수 있음

ex) [abc] , [a-zA-Z],[0-9]

문법 2) 메타 문자

”.” : 전체를 의미(아무거나 라는 뜻)

“*” : 앞에 있는 글자 반복 가능

”+” : 앞에 있는 글자 1회이상 반복

{m,n} : 반복횟수 지정

”?” : 반복횟수 1회

: or

^ : not

ex)

[0-9]{1,3}

01[0-1]?-[0-9]{4}-[0-9]{4}

http로 시작, zip으로 끝나는 파일명 : (http)(.+)(zip)

*정규식 연습장

www.regexr.com

*정규식 파이썬에서 쓰기

re 모듈 사용

import re

import urllib.request
url = "https:// ~ "
html = urllib.request.urlopen(url)
html_contents = str(html.read())

id_results = re.findall(r"([a-zA-Z0-9]+\*\*\*)",html_contents)

search : 한개만 찾기

findall : 전체 찾기

추출된 형태는 tuple

XML

데이터의 구조와 의미를 설명하는 TAG(MarkUp)을 사용하여 표시하는 언어 (html도 마찬가지)

정규 표현식으로 parsing 가능

beautifulsoup이라는 parser로 파싱됨.

from bs4 import BeautifulSoup

#객체 생성
soup = BeautifulSoup(books_xml, "lxml")
#Tag 찾는 함수 find_all 생성
soup.find_all("author")

JSON

JavaScript Object Notation

간결성 : 기계/ 인간 모두 이해하기 편함

ㄴ 용량도 적고 코드로 전환도 쉬움

python의 dict type과 유사

파이썬에서는

json 모듈을 사용하여 손 쉽게 파싱 및 저장 가능

저장 및 읽기는 dict type 과 상호 호환 가능

read

 import json
 
 with open("jsonex.json", "r", encoding = "utf8") as f:
   contents = f.read()
   json_data = json_loads(contents)
   
 print(json_data["employees"])

write

import json

dict_data = {'a' = 'one', 'b' = 'two'}

with open("data.json", "w") as f:
  json.dump(dict_data, f)

카테고리:

업데이트:

댓글남기기