How to Extract values from a field and write them to a text file using Python?

1746
12
Jump to solution
07-11-2013 10:06 AM
ChristiNelson1
New Contributor III
Hi all,

I am using ArcGIS 10.0 Python 2.6. I need to get the values from a single field (APN) out of a PARCEL feature class attribute table and write them to a text file.  The feature class contains about 20 different fields.  I only need to write the values from APN.

The only info I can find is outdated.  Can anyone help?

Thanks,
Christi Nelson
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
PhilMorefield
Occasional Contributor III
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()

View solution in original post

0 Kudos
12 Replies
by Anonymous User
Not applicable
This is untested but I think it should do the trick.


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)
0 Kudos
PhilMorefield
Occasional Contributor III
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()

View solution in original post

0 Kudos
T__WayneWhitley
Frequent Contributor
More than one way to do this, but I suppose the easiest (not necessarily the most efficient) is to use:

Export Feature Attribute to ASCII (Spatial Statistics)
Resource Center » Professional Library » Geoprocessing » Geoprocessing tool reference » Spatial Statistics toolbox » Utilities toolset
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005p0000003v000000


...also, the input can be a feature layer, meaning if you wanted you should be able to 'pare down' the output fields with the field info control.  This is available with the Make Feature Layer tool -- an example of how to use the field info control in a script is with the webhelp on Make Table View, so if you need that, it's here (and it should be used the same way with Make Feature Layer):

Make Table View (Data Management)
Resource Center » Professional Library » Geoprocessing » Geoprocessing tool reference » Data Management toolbox » Layers and Table Views toolset
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006v000000


Enjoy,
Wayne


Edit:  It's even easier than that!  You don't need Make Feature Layer, you can specify what you wish in the 2nd param (Value_Field) of the Spatial Statistics tool.  One tool!  (if you don't mind the additional XY coords)
0 Kudos
ChristiNelson1
New Contributor III
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()


Thank you! 

This worked seamlessly.  Funny, I did read section 7.2 in the text you provided, but hit a road block trying to incorporate it with a search cursor.

Thanks for all of your help, everyone!
Christi
0 Kudos
ChrisSnyder
Regular Contributor III
The .next() statement (in addition to the for loop) is unneccessary. I think a bit cleaner code would look like:

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()


Also, in v10.1 syntax using the .da cursors (~30x faster than the old search cursors):

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()
0 Kudos
T__WayneWhitley
Frequent Contributor
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!
0 Kudos
ChristiNelson1
New Contributor III
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!

Done!  Both answers were terrific.
0 Kudos
by Anonymous User
Not applicable
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!
0 Kudos
RhettZufelt
MVP Regular Contributor
For comparison.  run multiple times

>>>
took  5.42300009727  seconds to run using cursors
took  14.9790000916  seconds to run using Export_stats
took  1.01900005341  seconds to run using da.searchcursor
>>> ================================ RESTART ================================
>>>
took  5.4279999733  seconds to run using cursors
took  15.0590000153  seconds to run using Export_stats
took  1.11499977112  seconds to run using da.searchcursor
>>> ================================ RESTART ================================
>>>
took  6.02999997139  seconds to run using cursors
took  15.8759999275  seconds to run using Export_stats
took  1.08899998665  seconds to run using da.searchcursor
>>> ================================ RESTART ================================
>>>
took  5.89599990845  seconds to run using cursors
took  15.3450000286  seconds to run using Export_stats
took  1.04600000381  seconds to run using da.searchcursor
>>> ================================ RESTART ================================
>>>
took  6.25199985504  seconds to run using cursors
took  15.6540000439  seconds to run using Export_stats
took  1.0609998703  seconds to run using da.searchcursor
>>>


I find it interesting that using a single tool take so much longer...

R_
0 Kudos