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"))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 23, 2020 at 17:24
1
  • Which ArcGIS version do you have? Commented Jun 24, 2020 at 5:48

2 Answers 2

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

answered Jun 24, 2020 at 2:05
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.

answered Jun 23, 2020 at 17:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.