(Dec-20-2022, 05:12 AM)deanhystad Wrote: NaN will just be a string. Whatever usesthe generated csv file will have to know that the string "NaN" means NaN. What you want to write to the csv file in place of nulls will depend on what is using the generated csv file.
Unfortunately I found out the process that'll use generated csv file needs everything (including empty strings) in quotes except DB NULL values. So for DB values:
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 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)