Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If at all possible, migrate to using 10.1 where you get <code>arcpy.da</code>, a significantly more performant API for cursors. <a href="http://arcpy.wordpress.com/2012/07/12/getting-arcpy-da-rows-back-as-dictionaries/" rel="noreferrer">I've written a log post on this very topic of getting dictionaries back</a>. Geometries will all be the same because it uses a recycling cursor internally, so in 10.0 you'll want to grab <code>shape.__geo_interface__</code> instead and use <code>AsShape</code> to get it back to a geometry object.</p> <p>The order you get rows back is fairly arbitrary: you can expect it to be the same every time in a shapefile <em>without</em> a where clause and that's pretty much it, so your two-pass approach won't really be reliable.</p> <p>All this considered, you can do something like this:</p> <pre><code>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)] # get field list features = list(cursor_to_dicts(arcpy.SearchCursor(fc), fields)) </code></pre> <p>The magic is the <code>getattr()</code> call -- try to grab <code>value.__geo_interface__</code> if it exists, otherwise just default to <code>value</code>.</p> <p>As this question isn't really about the Python language in general but an API (<code>arcpy</code>) specific to GIS, you might be better off asking things like this on gis.stackexchange in the future.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload