I am trying desperately to grab records in a shapefile's attribute table and copy them, with additional formatting to a txt file. Below is the code I have developed thus far:
I have used the following post (Using SearchCursor to access & export values in a raster table) for guidance but for some reason python has issues with my declared output file, f.
# Import arcpy and the SeachCursor function
import arcpy
# Find the target shapefile in memory
shapefile = "C:/Users/user/Dropbox/CE 691/GI/GISData/ExampleEPANETNetwork.mdb/Nodes"
# Create the Text File to write the Indicated Values
f = open("C:/Users/user/Desktop/test.txt", "w")
# Use SearchCursor to Identify the necessary fields
find_junction = arcpy.SearchCursor(shapefile, "", "", "Elevation;Demand;X_Coord;Y_Coord","")
# Convert Found Field to Text and Place in Text File
for row in find_junction:
get_elevation = row.getValue("Elevation")
get_demand = row.getValue("Demand")
get_x_coord = row.getValue("X_Coord")
get_y_coord = row.getValue("Y_Coord")
print get_elevation, get_demand, get_x_coord, get_y_coord
f.write(str(get_elevation + " " + get_demand + " " + get_x_coord + " " + get_y_coord)
# Close the output text file
f.close()
# Delete all variables utilized
del find_junction, get_elevation, get_demand, get_x_coord, get_y_coord
Any advice would be very much appreciated!
Thanks!
+ "\n") at the end of your line. Also, consider instead using thecsvmodule which makes this type of file writing much cleaner. – blah238 Mar 4 at 2:12