user2437648
user2437648

Reputation: 75

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

`

Upvotes: 3

Views: 596

Answers (2)

Dan Gittik
Dan Gittik

Reputation: 3850

The string representation of None is actually 'None', not the empty string. Try:

"... ('%s', '%s')" % (A[i][0], A[i][1] if A[i][1] else '')

Upvotes: 0

woozyking
woozyking

Reputation: 5220

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.

Upvotes: 3

Related Questions