Search code examples
pythoncsvcursorblank-line

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?


Solution

  • Python 2.7:

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

    Python 3.x:

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