I need to extract a list of selected objects from several layers to text file. Is there any script or toolbox available?
Solved! Go to Solution.
Since this is the ModelBuilder space, I thought I'd go ahead and provide some code to use with the Calculate Value tool that would do this. This assumes you have model elements named 'Feature layer', 'Field', and 'Output text file'. If 'Feature layer' has a selection active on it, only field values from the selected rows would be written to the text file.
UPDATE added needed trailing new lines and return value
# Expression
f(r"%Feature layer%", "%Field%", r"%Output text file%")
# Code Block
def f(tbl, field, textfile):
# extract data to a list of text strings (with trailing newlines)
lst = []
with arcpy.da.SearchCursor(tbl, field) as rows:
for row in rows:
lst.append(str(row[0]) + chr(10))
# unique-ize and sort the list
lst = sorted(list(set(lst)))
# write to file
with open(textfile, "w") as f:
f.writelines(lst)
return textfile
# Data Type
File
You can export the table contents as a text file manually, but there is no script tool that exists. However, the python code for this process does exist, but you will need to code and set up the parameters yourself. The following documentation should help you get started: https://support.esri.com/en/technical-article/000011842
But to extract the contents to text manually, you can follow the steps in the following documentation. http://desktop.arcgis.com/en/arcmap/10.6/manage-data/tables/exporting-tables.htm
Be sure to select the version of ArcMap you are using at the top to ensure the correct steps are provided.
I hope this points you in the right direction.
Since this is the ModelBuilder space, I thought I'd go ahead and provide some code to use with the Calculate Value tool that would do this. This assumes you have model elements named 'Feature layer', 'Field', and 'Output text file'. If 'Feature layer' has a selection active on it, only field values from the selected rows would be written to the text file.
UPDATE added needed trailing new lines and return value
# Expression
f(r"%Feature layer%", "%Field%", r"%Output text file%")
# Code Block
def f(tbl, field, textfile):
# extract data to a list of text strings (with trailing newlines)
lst = []
with arcpy.da.SearchCursor(tbl, field) as rows:
for row in rows:
lst.append(str(row[0]) + chr(10))
# unique-ize and sort the list
lst = sorted(list(set(lst)))
# write to file
with open(textfile, "w") as f:
f.writelines(lst)
return textfile
# Data Type
File
thank you
Currently I've got list in 1 row (e.g FairfieldHartfordLitchfieldMiddlesex.....)
is it possible to gel list in separate row for each object?
(e.g Fairfield
Hartford
Litchfield
Middlesex.....)
thanks
Sorry, look above for an update, I modified the code to add the newlines (chr(10)) for you. (Unfortunately you can't use "\n" inside a Calculate Value code block you need to use chr(10) instead.)
thanks, now it works