Python Forum
Write Null values as 0.0 (float) type in csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write Null values as 0.0 (float) type in csv
#1
Hi Team,

with the below code I am extracting sql table data into CSV File with delimiter '|'

in SQL Table,in some fields ['Market Price',"Spread"] it has null Records. So for these values I want to
write values 0.0 as float(data type).

writer.writerows(row)


qry = "select * from Trading_data" 
cursor.execute(qry) 
data = cursor.fetchall() 
 
With open("E:\\backup\\output.csv","w","newline="") as outfile
    writer = csv.writer(outfile,quoting = csv.QUOTE_NONNUMERIC)
    writer.writerows(col[0] for col in cursor.description)
    While True:
        rows = cursor.fetchmany(10000)
        if len(rows) ==0:
            print("no records found")
            break
        else:
             for row in rows:
             	writer.writerows(row)
Reply
#2
So, change the nulls into 0s before writing.
Reply
#3
hi very nice,

how to do it here,

its complete row, but I want it to apply it in columns.
check value in two columns, and run the code

for row in rows:
writer.writerows(row)
Reply
#4
Use pandas instead of csv. When reading the csv into a DataFrame you can use DataFrame.fillna() to replace NaN with some value.

I don't understand why you are using csv files at all. From all your posts it sounds like you are getting information from a database and writing it to a CSV file only to read the CSV file and store the information in a database. Why?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 307 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  How to express null value klatlap 3 868 Mar-25-2023, 10:40 AM
Last Post: klatlap
Photo How to select NULL and blank values from MySQL table into csv python300 9 2,455 Dec-27-2022, 09:43 PM
Last Post: deanhystad
  formatting float like int for whole values Skaperen 8 1,760 Apr-11-2022, 01:56 PM
Last Post: deanhystad
  Float Slider - Affecting Values in Column 'Pandas' planckepoch86 0 1,404 Jan-22-2022, 02:18 PM
Last Post: planckepoch86
  value null when update in json file 3lnyn0 6 3,293 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,098 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Multiple conditions, one is null moralear27 1 2,204 Sep-13-2020, 06:11 AM
Last Post: scidam
  I didnt get the NULL values salwa17 0 1,590 Jul-10-2020, 02:54 PM
Last Post: salwa17
  Find only the rows containing null values Bhavika 2 2,447 Jun-10-2020, 01:25 PM
Last Post: Bhavika

Forum Jump:

User Panel Messages

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