Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep blank cell blank instead of None by python

I have a access table like this (Date format is mm/dd/yyyy)

col_1_id-----col_2

1 1/1/2003

2

3 1/5/2009

4

5 3/2/2008

Output should be a table where Co_1 is between 2 to 4 (Blank cell must be blank)

2

3 1/5/2009

4

I tried with sql query. The output print 'None' in blank cell. I need blank in blank cell. Other thing is when I tried to insert this value in another table it only insert rows having date value. The code stops when it gets any row without date. I need to insert rows as it is.

I tried in python with

import pyodbc
DBfile = Data.mdb
conn = pyodbc.connect ('Driver = {Microsoft Access Driver (*.mdb)}; DBQ =' +DBfile
cursor = conn.cursor()

sql_table = "CREATE TABLE Table_new (Col_1 integer, Col_2 Date)"
cursor.execute.sql_table()
conn.commit()

i = 0
while i => 2 and i <= 4:
    sql = "INSERT INTO New_Table (Col_1, Col_2)VALUES ('%s', '%s')" % (A[i][0], A[i][1])
    cursor.execute(sql)
    conn.commit()
i = i + 1
cursor.close
conn.close

`

like image 974
user2437648 Avatar asked Mar 09 '26 02:03

user2437648


1 Answers

Instead of using A[i][x] which dictates the value for you, why not simply add an OR logic to eliminate the possibility of None.

For any cell you wish to keep as "blank" (assume you mean empty string), let's say A[i][1], just do

A[i][1] or ""

Which will yield empty string "" if the cell gives you None.

like image 183
woozyking Avatar answered Mar 10 '26 16:03

woozyking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!