개발을 하다보면 CSV 파일에 있는 데이터를 읽어서 사용하는 경우가 있다.
파이썬으로는 역시나 간단하게 구현이 가능하다.
우선 간단한 샘플 데이터 준비
hello,my,name,is,Lee
this,is,the,example,code
파이썬으로 구현한 샘플 코드는 아래와 같다.
import csv
def read_csv(file_path):
data = []
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
data.append(row)
return data
# Example usage
csv_file = "./file.csv"
csv_data = read_csv(csv_file)
for row in csv_data:
print(row)
출력
['hello', 'my', 'name', 'is', 'Lee']
['this', 'is', 'the', 'example', 'code']
반응형