John_Rodgers
John_Rodgers

Reputation: 181

Write to CSV - Multiple blank rows created

I'm very new at Python and I trying to insert data from SQL to a CSV. I have the following code:

sql = cursor.execute("SELECT field_a, field_b, field_c FROM TableA LIMIT 500")
    result=cursor.fetchall()
    fp = open('dataset.csv', 'w')
    myFile = csv.writer(fp)
    myFile.writerows(result)
    fp.close()

But when I see the CSV created I have multiple blank lines. What I am doing wrong?

Upvotes: 0

Views: 78

Answers (1)

Dmitrii K
Dmitrii K

Reputation: 267

Python 2.7:

fp = open('dataset.csv', 'wb')

Python 3.x:

fp = open('dataset.csv', 'w', newline='')

Upvotes: 1

Related Questions