I just wanted to point out that just using if not row.Stand
may yield undesired results (remember, explicit is better than implicit from the Zen of Python). Take the following example:
>>> for i, sample in enumerate(['a',1,None, '', ' ', 0]):
if not sample:
print 'value "{}" at index {} is empty'.format(sample, i)
value "None" at index 2 is empty
value "" at index 3 is empty
value "0" at index 5 is empty
We can see here that a single space returns true, because it is a character. We are also removing zeros (which may or may not be a "NULL" value, depends on the situation).
Instead, it may be better to test if it is None
or if the string contains any non-whitespace characters:
>>> for i, sample in enumerate(['a',1,None, '', ' ', 0]):
if sample is None or not str(sample).strip():
print 'value "{}" at index {} is empty'.format(sample, i)
value "None" at index 2 is empty
value "" at index 3 is empty
value " " at index 4 is empty
Now we have removed any values that are just white space. This may not be a big issue, but me having prior work experience in an assessors office, there were plenty of parcel fields that were supposed to have values and only contained white space would not be flagged in the simple if not row.Field
example. Here is what you can use to test for a single field:
rows = arcpy.SearchCursor(fc,"","","","")
for row in rows:
if row.Stand is None or not str(row.Stand).strip():
print 'empty row!'
break
Or, if you want to test all fields:
import arcpy
fc = r'C:\TEMP\Huxley.gdb\Watermain\WtrValve'
fields = dict((f.name, []) for f in arcpy.ListFields(fc) if not f.required)
rows = arcpy.SearchCursor(fc,"","","","")
for row in rows:
for f in fields.keys():
fields[f].append(row.getValue(f))
for field, values in fields.iteritems():
if any(map(lambda s: s is None or not str(s).strip(), values)):
print 'Field: "{}" has empty values'.format(field)
This yielded:
>>>
Field: "VAL_TYPE" has empty values
Field: "GNSS_HEIGHT" has empty values
Field: "HL_ACCU" has empty values
Field: "PHOTO_1" has empty values
Field: "COMMENTS" has empty values
Field: "PROJ_NUMB" has empty values
Field: "PDF_LOC" has empty values
Field: "YR_INST" has empty values
Field: "TOP_COND" has empty values
Field: "VAL_USE" has empty values
Field: "ASBUILT" has empty values
Field: "PROJ_NAME" has empty values
I used this example in class the other night, it may offer a clue, as it works, basically copies fc out of a dataset, into a folder as shapefiles:
import arcpy
arcpy.env.workspace = "E:/class_5/ForExercise.gdb"
out = "E:/class_5/shp/"
datasets = arcpy.ListDatasets("*", "Feature")
for i in datasets:
print i +" DataSet Name"
fclist = arcpy.ListFeatureClasses('*', "ALL", i)
for x in fclist:
print x +" fc in Dataset"
arcpy.CopyFeatures_management(x, out + x)
print "Done"
Your error shows a path of ..\test1.gdb\SanTest with the slashes wrong unless you used the "r" in-front of the path. Better yet just flip the slashes on your source.
(fc, os.path.join(target_path, os.path.splitext(fc)[0])) The splitext(fc)[0} is how you remove the .shp off of a shapeFile. The fc in the database probably already has this removed. Another option to define the output is path + os.sep + fc
Best Answer
If you look at ESRI'S field map documentation, it seems the good way to specify fields is with a arcpy.FieldMap() object. The second example on the documentation seems to fit your needs :