1

Apparently, I cannot close the csv file, so that it cannot be opened to be processed. How can I solve my problem?

I get a list (A) from my calculations such that:

A = [[1.0,153.0016399,-27.6402685],[2.0,153.0783324,-27.4669576]]

I want to save it as csv file in order to read it and create a shapefile from XY. Creating csv file is successful and it is saved correctly, however it can not be immediately used to create a shapefile. When I copy this csv file into another file, it works fine. I need to do it 1000 times so I cannot copy it each step. My csv file looks like:

ID,x,y
1.0,153.0016399,-27.6402685
2.0,153.0783324,-27.4669576

and here is my code:

import csv
import arcpy
def MakeXY(x):        
    arcpy.env.workspace = "C:\\...\\"
    arcpy.env.overwriteOutput = True
    outWorkspace = "C:\\...\\"
    try:
        in_Table = x
        x_coords = "x"
        y_coords = "y"
        out_Layer_0 = "test"
        spRef = r"C:\\...\\Coordinate Systems\\WGS 1984.prj"

        # Make the XY event layer
        arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer_0, spRef)
        # Save to a layer file
        arcpy.FeatureClassToShapefile_conversion(out_Layer_0, outWorkspace)          
    except Exception as err:
        print(err.args[0])
A = [[1.0,153.0016399,-27.6402685],[2.0,153.0783324,-27.4669576]]
A_header = ['ID','x','y']           
writer = csv.writer(file(os.path.join('C:\\...\\', 'Mylist.csv'),'wb'))
writer.writerows([A_header])
writer.writerows(A)
f = open('C:\\...\\Mylist.csv')
f.close()
MakeXY('Mylist.csv')

and here is the error I get:

Failed to execute. Parameters are not valid. ERROR 000728: Field x does not exist within table ERROR 000728: Field y does not exist within table WARNING 000725: Layer Name or Table View: Dataset test already exists. Failed to execute (MakeXYEventLayer).

closed as off-topic by PolyGeo Aug 23 '17 at 1:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • What happens when you remove your try/except statements so that we can see the full error with its line number? – PolyGeo Dec 14 '16 at 8:06
  • As indicated by the accepted answer this is a pure Python question that has probably already been asked and answered at Stack Overflow. – PolyGeo Aug 23 '17 at 1:58
4

You are not opening/closing the file properly. This is how it should be done (check the csv help page):

A = [[1.0,153.0016399,-27.6402685],[2.0,153.0783324,-27.4669576]]
A_header = ['ID','x','y']
csv_path = os.path.join(r'C:\GIS\Temp', 'Mylist.csv')

with open(csv_path,'wb') as csv_file:
    data_writer = csv.writer(csv_file)
    data_writer.writerows([A_header])
    data_writer.writerows(A)

f = open(csv_path)
f.read().splitlines()
#['ID,x,y', '1.0,153.0016399,-27.6402685', '2.0,153.0783324,-27.4669576']    
f.close()

The error message says that you don't have x & y columns in .csv file. Make sure you do write and close the file properly using the with statement.

3

If you don't want to save a CSV file each time, then you can just use an InsertCursor directly.

import arcpy
import os

outWorkspace = r"C:\data"
sr = arcpy.SpatialReference(4326)

counter = 1
main_loop_that_does_calculaton:

    uniqueName = "calculation{0}.shp".format(counter)

    arcpy.CreateFeatureclass_management(outWorkspace, uniqueName, "POINT", spatial_reference=sr)
    arcpy.AddField_management(os.path.join(outWorkspace, uniqueName), "refID", "FLOAT")

    points = [[1.0,153.0016399,-27.6402685],[2.0,153.0783324,-27.4669576]]

    insertCursor = arcpy.da.InsertCursor(os.path.join(outWorkspace, uniqueName), ("SHAPE@XY", "refID"))

    for point in points:
        insertCursor.insertRow([(point[1], point[2]),point[0]])

    counter += 1
    del insertCursor

Without seeing the rest of your code, I'm not sure how to create the uniqueName for the shapefile. But you would just want to create a unique name based on the calculation you are doing. It could just be a simple counter.

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