I am trying to write into a text file using geometry tokens but they're not being called upon.
open(infile, "w")
cursor = arcpy.CreateFeatureclass(fc, ["OID@", "SHAPE@", "NAME"])
ID = 0
for row in cursor:
f = open("rivers_IWM.txt", "w")
f.write(str("OID@"))
f.write(str("SHAPE@X"))
f.write(str("SHAPE@Y"))
-
Which ArcGIS version do you have?Bera– Bera2020年06月24日 05:48:49 +00:00Commented Jun 24, 2020 at 5:48
2 Answers 2
You can write it in the following format:
import arcpy,os
from arcpy import env
ws = env.workspace = (r"F:\Ahmad\Test\PT") # Change the working directory
fc = "Cities.shp" # Change the shapefile
fld_list = ["OID@","SHAPE@x","SHAPE@y","Name"]
with open(os.path.join(ws,"cities.txt"),"w") as text_file:
with arcpy.da.SearchCursor(fc,fld_list) as cursor:
for row in cursor:
write_text = "{0}, {1}, {2}, {3}".format(row[0],row[1],row[2],row[3])
text_file.write(write_text + "\n")
Here is the output:
answered Jun 24, 2020 at 2:05
It looks like every time you pass a row in your cursor you are creating a new text file. You are basically re-writing the text file for each row. You also need to close you text file at the end of (and outside of) your for loop.
answered Jun 23, 2020 at 17:36
lang-py