If field is in database

762
4
05-03-2017 12:55 AM
JohnMcoy
New Contributor III

Hi everyone,

Maybe anyone knows how to check, if field exists is in FC, than return value from FC, if it`s not than value = Null.. Now i have fields dictionary.

fields = {"OBJECTID" : "ID", "SHAPE@X" : "POINTX", "field3" : "Field"}

I want to check if field3 are in FC, than i get field3 values, else, field not exists values = Null 

I think its IF/Else statement. Am I right ? 

 

Sorry if I ask basics questions. I`m a newbie in python programming....

Thanks for any help  

Tags (2)
0 Kudos
4 Replies
LukeWebb
Occasional Contributor III

You could list the fields, then compare the list to see if the field is within using an if statement like you said. 

What might also work is to just 'try' to grab the value, and use null if it fails for any reason. (the field doesnt exist, there is no value etc)

Cursor example

try:
    value = row.getValue('field')
except:
    value = None

JohnMcoy
New Contributor III

hi, 

So i have this code. In arcMap attributes table, columns field1 and field2 has <Null> values. So my question is, what statement should be, that in CSV file, which I exported,  the fields who don`t have values will be filled with NULL value.

with arcpy.da.SearchCursor(fc, ["OID@", "FIELD1", "FIELD2, "SHAPE@"]) as cursor:
         for (OID, FIELD1, FIELD2, SHAPE) in cursor:‍‍
             row = [OID, None, None, SHAPE]
             #Statement
             writer.writerow(row)
          ‍‍‍‍‍‍

Thanks !  

0 Kudos
JohnMcoy
New Contributor III

Never mind, I got the solution: 

for n,i in enumerate(row):
                if i==None:
                    row[n]="NULL"
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you just trying to replace None values with "NULL" before inserting the rows into another table/object?  If so, you can combine a Python Conditional Expression with a list comprehension to accomplish it in a bit more Pythonic way:

with arcpy.da.SearchCursor(fc, ["OID@", "FIELD1", "FIELD2, "SHAPE@"]) as cursor:
         for row in cursor:
             row = ["NULL" if i is None else i for i in row]
             #Statement
             writer.writerow(row)
0 Kudos