Arcpy String field getting set to None instead of Null

1791
5
04-16-2020 12:25 AM
RAHULPANDIA
New Contributor II

The code given below is setting value of the field as 'None' instead of Null?

import arcpy
fc = 'c:/data/base.gdb/well'
fields = ['Name']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        row[0] = None
        cursor.updateRow(row)

arcmap version 10.2

I need to set these string values to null ?

0 Kudos
5 Replies
MarcoBoeringa
MVP Regular Contributor

None is more or less the Python equivalent of NULL in a database. You are writing Python code, not SQL, here.

Unless you are executing SQL through ODBC with something like pyodbc, and emmitting queries like

<FIELD> IS NULL

you generally would only use None in Python code.

If you want to insert empty strings instead of None, you could simply use

row[0]=''

0 Kudos
RAHULPANDIA
New Contributor II

I know i can set this to ''....but  need it to set to nul...not None not ''...anyidea how to do it?

0 Kudos
MarcoBoeringa
MVP Regular Contributor

If you are working with a File Geodatabase, like you do, then setting NULL means using None in Python, there is no NULL concept for setting values in a File Geodatabase to None / NULL, you should use None instead as in the code example.

Remember: You are writing Python code here, which is not the same as defining SQL statements for use in ODBC.

If you are working with an Enterprise Geodatabase and writing DML SQL statements and sending them through e.g. ODBC, then yes, you can set the values to NULL, in a DML SQL statement.

0 Kudos
DanPatterson_Retired
MVP Emeritus

try indenting updaterow by one level as in the code examples here

UpdateCursor—Data Access module | Documentation 

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        row[0] = None
        cursor.updateRow(row)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Make sure the field allows NULL.  It could be that ArcGIS is converting None to 'None' if NULL is not allowed in the field.

0 Kudos