If possible, move on to using 10.1, where you get arcpy.da , a significantly more efficient API for cursors. I wrote an entry in this thread about returning dictionaries . The geometry will be the same because it uses an internal recirculation pointer, so at 10.0 you will want to take shape.__geo_interface__ instead and use AsShape to return it to the geometry object.
The order in which you get the lines back is pretty arbitrary: you can expect it to be the same every time in the shapefile without a where clause, and that is pretty much why your two-pass approach won't really be reliable.
All of this is covered, you can do something like this:
def cursor_to_dicts(cursor, field_names): for row in cursor: row_dict = {} for field in field_names: val = row.getValue(field) row_dict[field] = getattr(val, '__geo_interface__', val) yield row_dict fc = '/path/to/fc' fields = [f.name for f in arcpy.ListFields(fc)]
Magic - getattr() call - try to capture value.__geo_interface__ , if it exists, otherwise just default will be value .
Since this question concerns not only the Python language as a whole, but also the GIS-specific API ( arcpy ), you might be better off setting these things in gis.stackexchange in the future.
source share