본문 바로가기
Python

csv file 읽어 출력하기

by BeGeek 2018. 4. 23.

csv file읽어들이기 간단하다!

python 3.6.4 기준 아래와 같이 작성하였다.

csv파일을 open시 'rb' 타입으로 읽을때는

iterator should return strings, not bytes 오류가 발생하였음. 'rt'로 텍스트모드로 읽어서 해결 함.

 

csvfileload.py

 #csv데이터 불러오기

import csv

csvfile = open('data-text.csv' ,'rt') #rb대신 텍스트 모드로 rt로 읽음
reader = csv.DictReader(csvfile) #딕셔너리 타입으로 읽어들임

for row in reader:
    print (row)

 

댓글