import arcpy as ap # this is how you create a file in Python myOutputFile = open('C:\\temp\\myoutputfile.txt', 'w') # create a row object to navigate your table rows = ap.SearchCursor('C:\\temp\\myInputTable.shp') row = rows.next() # for each row in your table, retrieve the value in the 'APN' field, convert it # to a string, write it to your output file, then move to the next line in both # your input table and output file for row in rows: myOutputFile.write(str(row.APN) + '\n') del row, rows myOutputFile.close()
If only I could +10 you for using join()!
Just for fun, while talking about efficiency, may as well make to as few lines as possible 🙂 import arcpy fc = r'G:\Data\Geodatabase\Cedar_County.gdb\CADASTRAL\PARCEL' output = r'C:\Users\GIS\Desktop\textfile.txt' with open(output, 'w') as f: f.write('\n'.join(r.PID for r in arcpy.SearchCursor(fc))) print 'Created "%s"' %output @ Wayne, I didn't mention the da module because she mentioned she was on version 10.0 still...but as Chris said, the new .da cursors are waaaay faster! P.S. I need to limit my sugar intake while at work, feeling loopy...Happy scripting!
import arcpy fc = r'G:\Data\Geodatabase\Cedar_County.gdb\CADASTRAL\PARCEL' output = r'C:\Users\GIS\Desktop\textfile.txt' with open(output, 'w') as f: f.write('\n'.join(r.PID for r in arcpy.SearchCursor(fc))) print 'Created "%s"' %output
Christi, I had to give a point each to Caleb and Phil just because I thought it was neat to be able to compare the answers side by side --- both are good works!If you like Phil's work best, you probably ought to give him the 'green checkmark' - he deserves it.Enjoy,WaynePS - And a point to Chris for being so efficient.... I was wondering why noone mentioned the da (data access) module. 30x faster is impressive!
import arcpy myOutputFile = open('C:\\temp\\myoutputfile.txt', 'w') rows = ap.SearchCursor('C:\\temp\\myInputTable.shp') for row in rows: myOutputFile.write(str(row.APN) + '\n') del row, rows myOutputFile.close()
import arcpy myOutputFile = open('C:\\temp\\myoutputfile.txt', 'w') rows = arcpy.da.SearchCursor('C:\\temp\\myInputTable.shp',["APN"]) for row in rows: myOutputFile.write(str(row[0]) + '\n') del row, rows myOutputFile.close()
This is very easily done. You can read a little about SearchCursors here, and about writing to text files with Python here (page down to section 7.2 Reading and Writing Files.The general pattern for this operation in AGD 10.0 is something like this:import arcpy as ap # this is how you create a file in Python myOutputFile = open('C:\\temp\\myoutputfile.txt', 'w') # create a row object to navigate your table rows = ap.SearchCursor('C:\\temp\\myInputTable.shp') row = rows.next() # for each row in your table, retrieve the value in the 'APN' field, convert it # to a string, write it to your output file, then move to the next line in both # your input table and output file for row in rows: myOutputFile.write(str(row.APN) + '\n') del row, rows myOutputFile.close()
import arcpy def WriteFieldToText(table, field, text_file): # Open search cursor to loop thru table rows = arcpy.SearchCursor(table) f = open(text_file, 'w') for row in rows: f.write(str(row.getValue(field)) + '\n') del row, rows f.close() print 'Created "%s"' %text_file if __name__ == '__main__': fc = r'C:\Path\To_your\featureClass' field = 'APN' output = r'C:\Path\To_your\textfile.txt' # run function WriteFieldToText(fc, field, output)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.