我正在尝试从名为DEV_TYPE的字段中搜索访问表中的某些记录。我想将每条记录与我之前在脚本中构建的已知值列表进行比较:
(devcatList)
我想打印出列表中没有出现的任何值。记录中的一些值也是Null。我想将if语句设置为仅打印出列表中没有出现的值,但我也不想为Null值打印出“None”。我的脚本如下:
if field.name == "DEV_TYPE":
for iRow in arcpy.SearchCursor(fc):
if not iRow.DEV_TYPE is None or iRow.DEV_TYPE not in devcatList:
print str(iRow.OBJECTID) + " - " + str(iRow.DEV_TYPE)
我玩过'if not x is None
'到'if x is not None
'。已将“or
”更改为“and
”(即使它具有反直觉性),但我的打印输出要么返回所有值,要么不返回任何值或只返回“None
”....基本上我不想要的一切。我确定我做的事情很傻。有人可以指出我的愚蠢吗?
谢谢, 麦克
答案 0 :(得分:4)
我想你想要if iRow.DEV_TYPE is not None and iRow.DEV_TYPE not in devcatList:
答案 1 :(得分:2)
也许是这样的:
if field.name == "DEV_TYPE":
for iRow in arcpy.SearchCursor(fc):
dev_type = iRow.DEV_TYPE
if dev_type is not None and dev_type not in devcatList:
print "{} - {}".format(iRow.OBJECTID, iRow.DEV_TYPE)
if dev_type is not none
可以更方便(但不那么精确)陈述if dev_type
,因为None
在条件上下文中等同于False
。我也冒昧地以更惯用的方式重写print
陈述。