2016-12-10 1 views
1

CSV 파일에 쓰기위한 프로그램을 시험 중입니다.파이썬 3의 라인 스페이스가없는 CSV 파일에 쓰기

import csv 
#field names 
fields = ['Name','Branch','Year'] 
# data rows of CSV file 
rows = [['Stef', 'Multimedia Technology','2'], 
    ['Kani', 'Information Technology', '2'], 
    ['Plazaa', 'Electronics Engineering', '4'], 
    ['Lizzy', 'Computer Science', '4'], 
    ['Reshmi', 'Multimedia Technology', '3'], 
    ['Geetha','Electrical Engineering', '4'], 
    ['Keerti', 'Aeronautical Engineering', '3']] 

#writing to csv file 
#writing to csv file 
with open('records.csv','w') as csvfile: 
    #creating a csv writer object 
    csvwriter = csv.writer(csvfile) 
    #writing the fields 
    csvwriter.writerow(fields) 
    # writing the data rows 
    csvwriter.writerows(rows) 

프로그램이 잘 실행 :

여기 내 코드입니다. 그러나 CSV 파일에는 각 항목 사이에 빈 줄 바꿈 공간 (항목이없는 경우)이 입니다. 결과 CSV 파일에서 해당 줄을 제거하는 방법은 무엇입니까?

답변

2

Python3 설명서에 따라 권장되는 구현입니다.

with open('records.csv','w', newline='') as csvfile: 
    #creating a csv writer object 
    csvwriter = csv.writer(csvfile) 
    #writing the fields 
    csvwriter.writerow(fields) 
    # writing the data rows 
    csvwriter.writerows(rows) 
파이썬 2.7의 경우 무엇을해야 하는지를

https://docs.python.org/3/library/csv.html#csv.writer

+0

. 개행을 표시하거나 정의하지 않았습니다. –