Python Forum
python export to csv writes extra line between rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python export to csv writes extra line between rows
#1
I am having issues with the following export from sql table to csv. the output file is creating a extra line between each row, i need to resolve this. to include the headers and rows without extra line

import pyodbc
import csv
conn = pyodbc. connect("Driver ={sql native client};"
"server =myserver;
"Database = testdb;"
"trusted connection = yes")]
cursor = conn.cursor
sql1 = """select * from mytable"
cursor.execute(sql1)

with open('c:/test/myfile.csv', 'w')as output
    writer = csv.writer(output)
    writer.writerow(col[0] for col in cursor.description)
      for row in cursor:
          writer.writerow(row)
cursor.close()
Reply
#2
csv.writer.writerow() adds a newline at the end of the row. If the cursor is also doing that, then that's where the extra line is coming from. Does the csv output contain any commas on the "blank" lines? Or is it just an extra newline?

For the second case, try opening the file like this:
with open("c:/test/myfile.csv", "w", newline="") as output:
Reference: https://docs.python.org/3/library/csv.html#csv.writer
Reply
#3
thanks, however the problem i am now having is that i have a table with over 200 columns some are text coulmns max length up to 1200 char. when i run the above my output is overlapping, on some rows, how do i avoid this using python, I was having the same issue using sqlcmd to export to csv
Reply
#4
Example? I don't know what overlapping rows, in a text file, means.
Reply
#5
I have a table employee that has over 300 columns, some columns (HTML_COMMENT, REASON, DESCRIPTION) are text columns and the length could be over 1200 characters in length. what is happening is that when i run python or sqlcmd to extract the data from sql server table to csv file, because i am not specifying the length of the columns the exported values are not under the respective column headings.
example
SHOULD BE
ID,NAME,HTML_COMMENT(text field in sql server up to 1300 characters) ,....................(...... REPRESENTS ANOTHER 200 COLUMNS)
12,JOE,<YTF TESTETCWTCETC

ITS SHOWING UP AFTER EXPORT AS
ID,NAME,HTML_COMMENT(THE HTML_COMMENT value IS WRAPPING INTO next ROW INSTEAD OF STAYING UNDER THE HTML_COMMENT HEADER now its under ID)
<YTF TESTETCWTCETC
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 388 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 571 Aug-22-2023, 10:14 AM
Last Post: GYKR
Question Export Python output to Excel skyline1397 1 2,024 Jun-26-2022, 05:10 AM
Last Post: skyline1397
  multi-line CMD in one-line python kucingkembar 5 3,949 Jan-01-2022, 12:45 PM
Last Post: kucingkembar
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,624 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  writelines only writes one line to file gr3yali3n 2 2,369 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  Not able to add extra column to the list in the python shantanu97 2 1,646 Nov-17-2021, 10:14 AM
Last Post: snippsat
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,105 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,093 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  How to export from Python to Excel? jpy 4 6,347 Dec-23-2020, 03:26 PM
Last Post: jpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020