Solved! Go to Solution.
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)
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()
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 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()
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,
Wayne
PS - 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 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