I was working through an exercise designed to teach basic Python skills in ArcMap using Python 2.7. The file below consists of 34 pairs of x,y coordinates. The script creates a new empty polyline feature class, and then use this list of coordinates to create a new polyline. This requires the use of an insert cursor to create new rows and an array object to contain the point objects. Here is the original proposed script:
Python 2.7 script for ArcMap In the Spyder IDE with the ArcGIS Pro conda installation for 2.3, I hit a number of exceptions that I worked through as follows:
import arcpy
import fileinput
import string
import os
from arcpy import env
env.workspace = "C:/Workspace"
env.overwriteOutput = True
arcpy.CreateFeatureclass_management(r"C:/Results", "newfc", "POLYLINE", None, "DISABLED", "DISABLED", None, None, 0, 0, 0, None)
infile = "C:/coordinates.txt"
cursor = arcpy.da.InsertCursor("newfc.shp", ["SHAPE@"])
array = arcpy.Array()
for line in fileinput.FileInput(infile):
ID, X, Y = (float(infile.split(line," "))
array.add(arcpy.Point(X, Y))
cursor.insertRow([arcpy.Polyline(array)])
fileinput.close()
del cursor
However, there is a syntax error related to the arcpy.Array() method.
File "C:/create.py", line 21
array.add(arcpy.Point(X, Y))
^
SyntaxError: invalid syntax
Here is the point file.
My knowledge is limited and wondered if someone knows if this is a Python version issue and where to find more info, or if they have an answer to the syntax error problem I articulated above.
1 Answer 1
Try changing your for
loop to this:
for line in fileinput.FileInput(infile):
ID, X, Y = line.split()
array.add(arcpy.Point(float(X), float(Y)))
I'm not sure what you were trying to do with your infile/split/line line so I hope I've guessed your intentions correctly. In this case it simply splits each line (by any whitespace, by default). The cast from string to int or float is then done individually for the X and Y variables in-line on the next line.
Note that I've opted for floats rather than ints, as you would lose precision using ints, according to your screenshot of values... And because arcpy.Point expects floats (doubles).
-
@1969Paulie Don't forget to accept (green checkmark) the answer that helps you the most here.2019年10月14日 05:08:15 +00:00Commented Oct 14, 2019 at 5:08
line
in case its not what you are thinking.