Python Forum
How to select NULL and blank values from MySQL table into csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to select NULL and blank values from MySQL table into csv
#5
(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)
Reply


Messages In This Thread
RE: How to select NULL and blank values from MySQL table into csv - by python300 - Dec-27-2022, 04:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 858 Oct-03-2023, 10:25 PM
Last Post: lostintime
  How to express null value klatlap 3 1,010 Mar-25-2023, 10:40 AM
Last Post: klatlap
  Replacing values ​​in Mysql with a condition stsxbel 0 712 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  Mysql Workbench table not updated CatBall 2 1,254 Feb-13-2023, 05:37 PM
Last Post: CatBall
  Write Null values as 0.0 (float) type in csv mg24 3 1,639 Dec-07-2022, 09:04 PM
Last Post: deanhystad
  store all variable values into list and insert to sql_summary table mg24 3 1,295 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,874 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  value null when update in json file 3lnyn0 6 3,772 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,859 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,277 Jul-02-2021, 02:19 PM
Last Post: xtc14

Forum Jump:

User Panel Messages

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