Hi. I'm trying to learn how to read a txt file of coordinates into an arcpy script, and create a featureclass from those coordinates. The only examples I find in the community posts address/solve different aspects than where my script fails. I have copied the key steps directly from the Zandbergen (2013) text, which includes an exercise/code to convert a txt file into a polyline (I had hoped to creat a point file first, then a polyline, and then modify to create polygons from a different txt file.
My script is:
import arcpy
import fileinput
import string
import os
arcpy.env.workspace = r"E:\advAppScripting\testing\hawaii"
arcpy.env.overwriteOutput = True
out_path = r"E:\advAppScripting\testing\hawaii"
new_fc = "myline.shp"
fc_geom = "POLYLINE"
newSR = 26904
try:
arcpy.CreateFeatureclass_management(out_path, new_fc, fc_geom)
# it should be possible to include the spatial reference as the final parameter in CreateFeatureclass, but it fails
# unless I remove it, and do the searate DefineProjection below...???
arcpy.DefineProjection_management(new_fc, newSR)
#I've confirmed successful operation ofhte script though line 16..
infile = r"E:\advAppScripting\testing\hawaii\coordinates.txt"
cursor = arcpy.da.InsertCursor(new_fc, ["SHAPE@"])
array = arcpy.Array()
for line in fileinput.input(infile):
ID, X, Y = string.split(line," ")
# I think this is supposed to read each line of the infile and parse it as the column headings in the new_fc.
# generates a "module 'string' has no attribute 'split'" error message
cursor.insertRow([arcpy.Polyline(array)])
# I thought this line should be part of the iteration through the rows, but in the example I've followed, its not indented
fileinput.close()
del cursor
except Exception as e:
print("Error: " + e.args[0])
I think the problem may be that the string.split() is 'old Python' and no longer works,
but I'm unsure how to resolve.
Would appreciate any help/advice. I am attaching the associated txt file