I have a feature class table that i need to export to excel, the feature class will be created weekly. The problem i am having is that on creation the feature class gets the date added to the end of the feature class name and i am trying to access with a wildcard because the date will change based on when the feature class gets created. How can i get the feature class with a wildcard?
import arcpy
workspace = "Database Servers\GIS.gds\GISSERVER1 (VERSION:dbo.DEFAULT)"
arcpy.env.workspace = workspace
'''
lyrs = ['Hyrdo_FINAL_ALL_*']
for fc in arcpy.ListFeatureClasses():
for lyr in lyrs:
if fc.startswith(lyr):
print 'lyr'
'''
lyr = "Hyrdo_FINAL_ALL_*" #Database Servers\GIS.gds\GISSERVER1 (VERSION:dbo.DEFAULT)\Hyrdo_FINAL_ALL_121817
arcpy.MakeFeatureLayer_management(lyr, "In_memory\lyr1")
arcpy.TableSelect_analysis("In_memory\lyr1","C:/Temp/Export_Output.xls", "")
print 'done'
Hi CC,
ListFeatureClasses has an optional wildcard parameter I think you could use.
arcpy.env.workspace = r"[path to your workspace]"
lyr = arcpy.ListFeatureClasses("Hyrdo_FINAL_ALL_*")[0]
# do something with lyr
By the way, I don't think an excel file is a valid output to TableSelect. If you just want to write the table to an excel spreadsheet you could do:
arcpy.TableToExcel_conversion(lyr, r"[path].xls")
Hope this helps!
Micah
I am getting "list index out of range" at lyr = arcpy.ListFeatureClasses("Hyrdo_FINAL_ALL_*
") [0]?
thanks for the TableToExcel.
did you then try Randy's suggestion of 'startswith' that would be the next extension... assuming Hyrdo is the correct spelling and not Hydro
Hmmm, it's possible that would due to a typo. Is the spelling "Hyrdo_FINAL_ALL_..." or "Hydro_FINAL_ALL_..."?
Also, is the old feature class deleted and replaced by the date-stamped new copy, or is a date-stamped new copy added to the workspace? If there is a stack of them going back multiple weeks you might need to parse the date field like Randy said to find the newest one.
Micah
First, I would use a date format that is suitable for sorting: YYMMDD. Append this to your feature names when backing up. You can then append the feature name to a list (if it starts with, contains, etc. what you are looking for) when you loop through a ListFeatureClasses or ListLayers, sort the list and then pick the first or last item in the sorted list as the oldest or newest.
features = ['Hyrdo_FINAL_ALL_171218','Hyrdo_FINAL_ALL_171012',
'My_Feature_170101', 'Another_Feature_161012']
found = []
for feature in features:
if feature.startswith('Hyrdo_FINAL_ALL_'): # assumes this is name up to date
found.append(feature)
found.sort()
print found[-1:]
Yes Hydro, sorry. The feature class gets delete and replace with a new one with the date it was created, sorry i should have mentioned that.
did you fix this
lyr = "Hyrdo_FINAL_ALL_*" #Database Servers\GIS.gds\GISSERVER1 (VERSION:dbo.DEFAULT)\Hyrdo_FINAL_ALL_12181
and try it again given the spelling error (or is it?)
Fixing the typo didn't not fix it the "list index out of range" error.
that error message is only returned when there is an empty list...
for example
a = [] # define an empty list
a[0] # slice the empty list and get an error
Traceback (most recent call last):
File "<ipython-input-18-6a1284577a36>", line 1, in <module>
a[0]
IndexError: list index out of range
Now you say... but the list isn't empty...
Another way to get the error
a = [1,2] # so now we have 2 elements in the list...
a[2] # so let's get the second
Traceback (most recent call last):
File "<ipython-input-21-fc907be37984>", line 1, in <module>
a[2]
IndexError: list index out of range
But python is zero-indexed, so if you want the second, then you have to slice by [1], since [0] returns the first.
That's how slicing works... it is either empty or you sliced beyond the range, or you sliced wrong.