使用 python 查找空值表单形状文件属性字段
Finding Null value form shape file attribute fields using python
我有 100 个来自不同子文件夹的形状文件。我想检查每个形状文件中的 "Population" 字段。
如果任何形状文件中的 "Population" 字段是 ["", " ", None] 然后打印这些特定形状文件的名称。
import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers"
pattern = 'mig*.shp'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
shp = os.path.join(root, filename)
if arcpy.ListFields(shp, "Population"):
print("{} has Population field".format(shp))
with arcpy.da.SearchCursor(shp, ["Population"]) as rows:
for row in rows:
if row[0] == ["", " ", None]:
print("{} has a empty value".format(shp))
else:
print "OK"
脚本可以很好地查找具有人口字段的形状文件名称。但是它无法找到人口字段为空的形状名称。
我得到 "runtime error"。类型错误:字符串索引必须是整数,而不是 str.
我无法准确回答你的问题,但也许这种方法会对你有所帮助:
import arcgisscripting
gp = arcgisscripting.create(9.3)
...
rows = gp.SearchCursor(shp)
row = rows.Next()
while row:
population = row.GetValue("Population")
# ... perform check ...
row = rows.next()
我宁愿将它添加为对你的问题的评论,但我不能。
首先,如果你想检查字段是否为空,请更改行:
if row[0] == ["", " ", None]:
至:
if row[0] in ["", " ", None]:
但这是不应导致运行时错误的逻辑错误。
还请确保最后一行打印 "OK" 的缩进是正确的。
运行 下面的代码,让我们知道运行时错误是否仍然存在。如果是,请提供更多信息(包括错误行):
import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers"
pattern = 'mig*.shp'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
shp = os.path.join(root, filename)
if arcpy.ListFields(shp, "Population"):
print("{} has Population field".format(shp))
with arcpy.da.SearchCursor(shp, ["Population"]) as rows:
for row in rows:
if row[0] in ["", " ", None]:
print("{} has a empty value".format(shp))
else:
print "OK"
我有 100 个来自不同子文件夹的形状文件。我想检查每个形状文件中的 "Population" 字段。 如果任何形状文件中的 "Population" 字段是 ["", " ", None] 然后打印这些特定形状文件的名称。
import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers"
pattern = 'mig*.shp'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
shp = os.path.join(root, filename)
if arcpy.ListFields(shp, "Population"):
print("{} has Population field".format(shp))
with arcpy.da.SearchCursor(shp, ["Population"]) as rows:
for row in rows:
if row[0] == ["", " ", None]:
print("{} has a empty value".format(shp))
else:
print "OK"
脚本可以很好地查找具有人口字段的形状文件名称。但是它无法找到人口字段为空的形状名称。
我得到 "runtime error"。类型错误:字符串索引必须是整数,而不是 str.
我无法准确回答你的问题,但也许这种方法会对你有所帮助:
import arcgisscripting
gp = arcgisscripting.create(9.3)
...
rows = gp.SearchCursor(shp)
row = rows.Next()
while row:
population = row.GetValue("Population")
# ... perform check ...
row = rows.next()
我宁愿将它添加为对你的问题的评论,但我不能。 首先,如果你想检查字段是否为空,请更改行:
if row[0] == ["", " ", None]:
至:
if row[0] in ["", " ", None]:
但这是不应导致运行时错误的逻辑错误。 还请确保最后一行打印 "OK" 的缩进是正确的。
运行 下面的代码,让我们知道运行时错误是否仍然存在。如果是,请提供更多信息(包括错误行):
import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers"
pattern = 'mig*.shp'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
shp = os.path.join(root, filename)
if arcpy.ListFields(shp, "Population"):
print("{} has Population field".format(shp))
with arcpy.da.SearchCursor(shp, ["Population"]) as rows:
for row in rows:
if row[0] in ["", " ", None]:
print("{} has a empty value".format(shp))
else:
print "OK"