![]() |
Write Null values as 0.0 (float) type in csv - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Write Null values as 0.0 (float) type in csv (/thread-38888.html) |
Write Null values as 0.0 (float) type in csv - mg24 - Dec-07-2022 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) RE: Write Null values as 0.0 (float) type in csv - ndc85430 - Dec-07-2022 So, change the nulls into 0s before writing. RE: Write Null values as 0.0 (float) type in csv - mg24 - Dec-07-2022 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) RE: Write Null values as 0.0 (float) type in csv - deanhystad - Dec-07-2022 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? |