email,birthyear,name,Location
laura@example.com,1996,Laura Grey,Manchester
craig@example.com,1989,Craig Johnson,London
mary@example.com,1997,Mary Jenkins,London
jamie@example.com,1987,Jamie Smith,Manchester
jhon@example.com,1998,John,Manchester
csv 파일을 다루기 위해 모듈 import
import csv
open함수를 이용해 csv파일을 열고, list 자료형으로 읽어온다.
csv_path = "sample.csv" csv_file = open(csv_path, "r", encoding="utf-8") csv_data = csv.reader(csv_file)
csv파일의 데이터를 콘솔창에 출력 후, 작업이 끝난 csv 파일을 닫아줍니다.
for i in csv_data: print(i) csv_file.close()
결과화면
# result output
"""
['email', 'birthyear', 'name', 'Location']
['laura@example.com', '1996', 'Laura Grey', 'Manchester']
['craig@example.com', '1989', 'Craig Johnson', 'London']
['mary@example.com', '1997', 'Mary Jenkins', 'London']
['jamie@example.com', '1987', 'Jamie Smith', 'Manchester']
['john@example.com', '1998', 'John', 'Manchester']
"""
csv 파일을 다루기 위해 모듈 import
import csv
csv를 dict 자료형으로 읽기
csv_file = open(csv_path, "r", encoding="utf-8") csv_data = csv.DictReader(csv_file)
csv파일의 데이터를 콘솔창에 출력 후, 작업이 끝난 csv 파일을 닫아줍니다.
for i in csv_data: print(i) csv_file.close()
결과화면
# result output """ {'email': 'laura@example.com', 'birthyear': '1996', 'name': 'Laura Grey', 'Location': 'Manchester'} {'email': 'craig@example.com', 'birthyear': '1989', 'name': 'Craig Johnson', 'Location': 'London'} {'email': 'mary@example.com', 'birthyear': '1997', 'name': 'Mary Jenkins', 'Location': 'London'} {'email': 'jamie@example.com', 'birthyear': '1987', 'name': 'Jamie Smith', 'Location': 'Manchester'} {'email': 'john@example.com', 'birthyear': '1998', 'name': 'John', 'Location': 'Manchester'} """
파이썬에서 csv 파일을 다루기 위해 모듈 import
import csv
newline='' 옵션을 줘서 중간에 공백 라인이 생기는 것을 방지합니다.
csv_path = "sample.csv"
csv_file = open(csv_path, "a", encoding="utf-8", newline='')
csv_writer = csv.writer(csv_file)
csv에 데이터를 추가합니다.
>
```python
csv_writer.writerow(["lee@sparta.com", '1989', "lee", "Seoul"])
데이터 추가 완료 후, 파일을 닫아줍니다.
csv_file.close()
파일이 제대로 입력되었는지 확인해봅니다
csv_file = open(csv_path, "r", encoding="utf-8") csv_data = csv.reader(csv_file) for i in csv_data: print(i) csv_file.close() # result output """ #output ['lee@sparta.com', '1989', 'lee', 'Seoul'] # 추가 된 행 """
파이썬의 함수를 장식해주는 역할
선언되는 함수 위에 골벵이를 사용해 @decorator 형태로 작성
해당 함수가 실행될 때 데코레이터에서 선언 된 코드가 같이 실행
데코레이터는 일반 함수와는 다른 특별한 구조
# 데코레이터는 호출 할 함수를 인자로 받도록 선언합니다. def decorator(func): # 호출 할 함수를 감싸는 wrapper 함수를 선언합니다. def wrapper(): # func.__name__에는 데코레이터를 호출 한 함수의 이름이 들어갑니다. print(f"{func.__name__} 함수에서 데코레이터 호출") func() print(f"{func.__name__} 함수에서 데코레이터 끝") # wrapper 함수를 리턴합니다. return wrapper
선언되는 함수 위에 @decorator를 추가해 데코레이터를 사용할 수 있다.
@decorator def decorator_func(): print("decorator_func 함수 호출") # decorator_func() # # result output """ decorator_func 함수에서 데코레이터 호출 decorator_func 함수 호출 decorator_func 함수에서 데코레이터 끝 """
인자(arguments)가 있는 함수의 데코레이터
# 입력받은 인자에 2를 곱해주기 def double_number(func): def wrapper(a, b): # 함수에서 받은 인자에 2를 곱해줍니다. double_a = a * 2 double_b = b * 2 return func(double_a, double_b) return wrapper # @double_number def double_number_add(a, b): return a + b # def add(a, b): return a + b # print(double_number_add(5, 10)) print(add(5, 10)) # result output """ 30 15 """