0

I'm having trouble finding a way to write the output of a SearchCursor function to a txt. file. I can print it just fine in a format that is desirable. I just don't know how to get from there to a txt. file. Here's my code below:

import arcpy
from arcpy import env
env.workspace = "G:\Geocomputation_Project\Section C\Lin_Ref_test"
shape = "Points.shp"
cursor = arcpy.SearchCursor(shape)
f = open("test_field.txt", "w")
for row in cursor:
    print (row.getValue("ITEMID")),(row.getValue("New_X_LRR")),  (row.getValue("New_Y_LRR")) 
print "done"

The txt file does not yet exist. When I start running this script, I hope to be able to overwrite the text file as I do more iterations.

I am using ArcMap 10.2.2

3
  • 2
    You should always use data access (DA) cursors with 10.1 or higher ArcGIS releases. The rest of this isn't GIS-centric, just how file I/O works in Python, and therefore more appropriate for StackOverflow. Please be sure to ask only one question per Question. – Vince Nov 20 '15 at 19:21
  • Yes, "w" overwrites it ("a" is for appending). Yes, you'll want a complete path for open. It's not aware of the arcpy workspace. This (+ many other questions) should get you started with dumping to disk. – Paul Nov 20 '15 at 19:22
  • There seem to be even more questions emerging in comments, and as @Vince said at the outset there should be only one question per question as per the Tour. – PolyGeo Nov 20 '15 at 21:01
1

I inserted the lines you require in between:

import arcpy
import os
from arcpy import env
env.workspace = r"G:\Geocomputation_Project\Section C\Lin_Ref_test"

# Delete prior output if it exists
# Insert a line that creates a variable for the text file
try:
    os.remove("C:\\temp\\output1.txt")
except OSError:
    pass
print("output1.txt deleted")
outtext = r'C:\temp\output1.txt'  # here supply your path and filename

# Then your code
shape = "Points.shp"
cursor = arcpy.SearchCursor(shape)
openfile = open(outtext, 'a')
for row in cursor:
    print row.getValue("ITEMID"), row.getValue("New_X_LRR"), row.getValue("New_Y_LRR") 
    # Then out put the stuff you want to print into the text file
    # (I used new line here you can change how you can concatenate
    # the rows. For example if you want to join them with a ','
    # then change the '\n' part into ','.)
    openfile.write(
        str(row.getValue("ITEMID")) + ' ' +
        str(row.getValue("New_X_LRR")) + ' ' +
        str(row.getValue("New_Y_LRR")) + '\n'
    )
print "done"
openfile.close()

I've found an efficient pythonic if exists line from the accepted answer here credit for that goes to SE user Matt

22
  • btw.. it doesn't help to have space in workspace paths..like the space between 'Section' and 'C'. "G:\Geocomputation_Project\Section C\Lin_Ref_test – yanes Nov 20 '15 at 19:28
  • 1
    Having spaces in workspace paths doesn't hurt (except in really strange circumstances). – Paul Nov 20 '15 at 19:43
  • 1
    @yanes, I wouldn't go as far to say it's "best practice" to not use spaces. Windows itself uses default system folders with spaces. – Paul Nov 20 '15 at 20:00
  • 1
    @nmpeterson thanks! i didn't see that. Also thank you for making it look readable. it was def. not PEP8 standard in its original format :) – yanes Nov 20 '15 at 20:32
  • 1
    @yanes this works phenomenally. Thank you very much for your expertise. – Stephen E Nov 20 '15 at 21:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.