티스토리 뷰

728x90

 

Hello, this is Dev_dog.

 

In this post, We're going to check the way

to get rid of blank lines generated every line when using csv.writer in Python.

 

If you find any errors or have any questions, feel free.

 

 한글 포스트는 아래 링크에서 보실 수 있습니다 : 

[KR/PYTHON] - [PYTHON3] CSV/TSV 매 행마다 공백줄 생성 문제 해결 방법

 

 

 

 


 

 

Development Environment

Windows 10

python 3.6.4

 

Problem

When creating a csv or tsv file through csv.writer in Windows environment,

an additional blank line is created between rows. (both same for csv and tsv when using csv.writer)

 

 

Problem - source

from datetime import datetime
import json
import csv
import os

# Make sample data for tsv file
head = [
        "Seq",
        "Name",
        "Kind",
        "Colour"
    ]
body = [
        [1, "Apple", "Fruit", "Red"],
        [2, "Banana", "Fruit", "Yellow"],
        [3, "Caterpillar", "Insect", "Green"],
        [4, "Dear", "Animal", "Brown"],
        [5, "Elephant", "Animal", "Grey"]
    ]

# Set file name with directory
filename = os.path.join("D:/blank.tsv")

# Write tsv file
with open(filename, "w") as result_file :

    # Using csv writer with delimiter '\t' which means tab separated(tsv) file
    tsv_writer = csv.writer(result_file, delimiter='\t')

    ## Wirte header
    tsv_writer.writerow(head)

    ## Write body
    for row in body :
        tsv_writer.writerow(row)

 

Reason

This problem occures when csv.writer is used on Windows.

 

csv.writer writes  \r\n  to the file,

but if it is not a binary mode, Windows makes  \n  →  \r\n 

which means it eventually turns into  \r\r\n .

 

 

Solution

with open(filename, "w") as result_file :

change the part above to below.

with open(filename, "w", newline="") as result_file :

 

Modified - source

from datetime import datetime
import json
import csv
import os

# Make sample data for tsv file
head = [
        "Seq",
        "Name",
        "Kind",
        "Colour"
    ]
body = [
        [1, "Apple", "Fruit", "Red"],
        [2, "Banana", "Fruit", "Yellow"],
        [3, "Caterpillar", "Insect", "Green"],
        [4, "Dear", "Animal", "Brown"],
        [5, "Elephant", "Animal", "Grey"]
    ]

# Set file name with directory
filename = os.path.join("D:/no_blank.tsv")

# Write tsv file
with open(filename, "w", newline="") as result_file :

    # Using csv writer with delimiter '\t' which means tab separated(tsv) file
    tsv_writer = csv.writer(result_file, delimiter='\t')

    ## Wirte header
    tsv_writer.writerow(head)

    ## Write body
    for row in body :
        tsv_writer.writerow(row)

 

 

Final Result

 

 


 

This post was written with the following reference.

 

Specially In case you are using Python2 :

https://sopython.com/canon/97/writing-csv-adds-blank-lines-between-rows/

 

Writing CSV adds blank lines between rows - sopython

The way Python handles newlines on Windows can result in blank lines appearing between rows when using csv.writer. In Python 2, opening the file in binary mode disables universal newlines and the data is written properly. with open('/pythonwork/thefile_sub

sopython.com

 

 

 

728x90
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
04-29 17:15