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) 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).