Export list of coordinates to file

465
2
Jump to solution
09-27-2013 12:22 PM
PauloBlanco
New Contributor
I'm trying to build a script to list the coordinates of contour lines and exporting them to an ASCII file. This is the code:

import arcpy  infc = arcpy.GetParameterAsText(0) # input feature class (polyline) zfield = arcpy.GetParameterAsText(1) # field with elevation value outfile = arcpy.GetParameterAsText(2) # output file  f = open(outfile, 'w')  for row in arcpy.da.SearchCursor(infc, ['SHAPE@', zfield]):     f.write('\n{elevation:12.3f}{vertices:12.0f}\n'.format(         elevation=row[1], vertices=row[0].pointCount)         )     for part in row[0]:         pntcount = 1         for pnt in part:             if pnt:                 if pntcount % 3 = 0:                     f.write('{0:12.1f}{1:12.1f}\n'.format(pnt.X,pnt.Y))                 else:                     f.write('{0:12.1f}{1:12.1f}'.format(pnt.X,pnt.Y))             pntcount += 1  f.close()


This is the output produced by the code:
       5.000           8    247822.0   6896884.5    248152.7   6896829.0    248210.9   6896754.9    248210.9   6896662.3    248128.9   6896590.9    248004.5   6896455.9    248168.6   6896154.3    248375.0   6896135.8      10.000           8    248697.7   6897106.8    248576.0   6896802.5    248351.1   6896553.8    248361.7   6896455.9    248470.2   6896326.3    248602.5   6896278.6    248758.6   6896321.0    248946.5   6896437.4


This is the desired output:
       5.000           8     247822.0   6896884.5    248152.7   6896829.0    248210.9   6896754.9     248210.9   6896662.3    248128.9   6896590.9    248004.5   6896455.9     248168.6   6896154.3    248375.0   6896135.8       10.000           8     248697.7   6897106.8    248576.0   6896802.5    248351.1   6896553.8     248361.7   6896455.9    248470.2   6896326.3    248602.5   6896278.6     248758.6   6896321.0    248946.5   6896437.4


All the info is extracted correctly, but the line breakers aren't working. Any ideas why? Thanks in advance.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StacyRendall1
Occasional Contributor III
                if pntcount % 3 == 0:
                    f.write('{0:12.1f}{1:12.1f}\n'.format(pnt.X,pnt.Y))
                else:
                    f.write('{0:12.1f}{1:12.1f}\n'.format(pnt.X,pnt.Y))


The == sign tells it to test if the left side is equal to the right side, = is an assignment... You are also missing the newline from the second statement. I am guessing that the top problem caused it to always end up in the second statement, which was missing the newline.

I have one other idea if these changes don't fix the problem, so please let me know.

View solution in original post

0 Kudos
2 Replies
StacyRendall1
Occasional Contributor III
                if pntcount % 3 == 0:
                    f.write('{0:12.1f}{1:12.1f}\n'.format(pnt.X,pnt.Y))
                else:
                    f.write('{0:12.1f}{1:12.1f}\n'.format(pnt.X,pnt.Y))


The == sign tells it to test if the left side is equal to the right side, = is an assignment... You are also missing the newline from the second statement. I am guessing that the top problem caused it to always end up in the second statement, which was missing the newline.

I have one other idea if these changes don't fix the problem, so please let me know.
0 Kudos
PauloBlanco
New Contributor
The = sign was indeed wrong, thank you.

However, what was really causing my problem is that the script somehow wasn't being correctly updated. I deleted the script in ArcGIS and reloaded it, and it worked - after changing the = sign.
0 Kudos