I am using the pandas library to write the contents of a mysql database to a csv file.
But when I write the CSV, every other line is blank:
Also, it's printing line numbers to the left that I do not want. The first column should be 'Account Number'.
Here is my code:
destination = 'output_file.txt'
read_sql = """ SELECT LinkedAccountId,ProductName,ItemDescription,ResourceId,UnBlendedCost,UnBlendedRate,Name,Owner,Engagement FROM billing_info ;"""
fieldnames = ['Account Number', 'Product Name', 'Item Description', 'Resource ID', 'UnBlended Cost', 'UnBlended Rate', 'Name', 'Owner', 'Engagement']
# Open the file
f = open(destination, 'w')
cursor.execute(read_sql)
while True:
# Read the data
df = pd.DataFrame(cursor.fetchmany(1000))
# We are done if there are no data
if len(df) == 0:
break
# Let's write to the file
else:
df.to_csv(f, header=fieldnames)
Why is it printing blank lines between the lines with data? How can I get it to create the file without blank lines and without the line number column to the left?
Writing CSV adds blank lines between rows. 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.
You’ve used the Pandas read_csv () and .to_csv () methods to read and write CSV files. You also used similar methods to read and write Excel, JSON, HTML, SQL, and pickle files. These functions are very convenient and widely used.
Using Print () function with a newline character To Print Blank Line In Python 5. Printing multiple blank lines Sometimes, while programming in Python, we came to a situation where we want to print the data in a new line, a blank line between two-lines, or print the data in the same line without using the newline.
Pandas IO tools can also read and write databases. In this next example, you’ll write your data to a database called data.db. To get started, you’ll need the SQLAlchemy package. To learn more about it, you can read the official ORM tutorial. You’ll also need the database driver. Python has a built-in driver for SQLite.
Have a look at the options for to_csv: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
For convenience, I have posted some items of interest here:
line_terminator : string, optional
The newline character or character sequence to use in the output file. Defaults to os.linesep, which depends on the OS in which this method is called (‘n’ for linux, ‘rn’ for Windows, i.e.).
And
index : bool, default True
Write row names (index).
Are probably what you're looking for. As for the empty lines, try explicitly specifying a single newline:
df.to_csv(f, header=fieldnames, index=False, line_terminator='\n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With