1

I am trying to select specific columns from a MySQL table into csv. This table has integers, varchar, NULL and empty strings. I want to differentiate between NULL and empty strings in specific way.

For DB row content: ABC, 123,,NULL,2022-12-22

The csv should have: "ABC"|"123"|""||"2022-12-22"

Is there any way to do this using Python without having to do row level operations?

I am trying to use Pandas and csv modules (open for other options as well) but couldn't find a way to represent DB NULL with nothing and empty string with "".

In csv I get either

"ABC"|"123"|""|""|"2022-12-22" #df.to_csv(r'file_name.csv', index=False, sep='|', quoting=csv.QUOTE_ALL, na_rep=None)

OR

"ABC"|123|""|""|"2022-12-22" #df.to_csv(r'file_name.csv', index=False, sep='|', quoting=csv.QUOTE_NONNUMERIC, na_rep=None)

OR

ABC|123|||2022-12-22 #df.to_csv(r'file_name.csv', index=False, sep='|', quoting=csv.QUOTE_MINIMAL, na_rep=None)

My complete code is:

import mysql.connector
import pandas as pd
import csv
 
mydb = mysql.connector.connect (
    host = "hostname",
    user = "user_name",
    password = "pwd",
    database = "db_name"
)
 
sqlquery = pd.read_sql_query('''select * from db_name.table_name''')
 
df = pd.DataFrame(sqlquery)
df.to_csv(r'file_name.csv', index=False, sep='|', quoting=csv.QUOTE_ALL, na_rep=None)

I have checked many SO posts like following but none give the solution I am looking for:

Writing-empty-string-with-quotes-and-null-value-without-quotes-while-writing-to

dont-convert-null-value-to-empty-string-when-exporting-mysql-data-to-csv

writing-a-pandas-dataframe-into-a-csv-file-with-some-empty-rows

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.