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
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
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 !
Never mind, I got the solution:
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)