I am trying to iterate through an excel spreadsheet using arcpy but having trouble querying fields that contain spaces in their names:
If I start by examining the fields by the python console I see this:
>>> source_path = r"\path\to\spreadsheet.xlsx\'sheet_name$'"
>>> fields = arcpy.ListFields(source_path)
>>> fields[1].name
u'Shire'
>>> fields[8].name
u'Area ha'
Then if I build a da.SearchCursor like this I am able to query "Shire" successfully:
>>> with arcpy.da.SearchCursor(source_path, fields[1].name) as cursor:
... for row in cursor:
... do_some_work(row)
But if I apply the approach to query "Area ha" it is unsuccessful:
>>> with arcpy.da.SearchCursor(source_path, fields[8].name) as cursor:
... for row in cursor:
... do_some_work(row)
Runtime error
Traceback (most recent call last):
File "<string>", line 2, in <module>
RuntimeError: Syntax error (missing operator) in query expression 'Area ha'.
This leads me to ask how to escape the space character in this context or otherwise achieve this query?
I am running:
- ArcGIS Desktop 10.3.1.4959
- Microsoft Office Access database engine 2007 12.0.1418.1031
_
in the space area in Excel and check if this can solve the problem? – ahmadhanb Feb 27 '18 at 7:54'*'
in the cursor. – BERA Feb 27 '18 at 9:57