0

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"))
1
  • Which ArcGIS version do you have? – BERA Jun 24 '20 at 5:48
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:

enter image description here

0
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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