I have a Python script that creates a point shapefile from a csv file within a Python script. This script gives me a ValueError for the longitude field (LON) if there isn't an extra nonsensical field to the right of it. Below is the error and how the csv file has been formatted to remove this error:
lonValueIndex = valueList.index("LON")
ValueError: list.index(x): x not in list
DATE LAT LON TEST
2/19/2011 34.27531 -118.21071 a
2/19/2011 34.18069 -118.34079 b
2/19/2011 34.0215 -118.21857 c
Do you see that Test field with values of a, b, and c? If I don't put a field to the right of the LON field I get the ValueError. Below is part of my script. Does anyone know why that extra field is necessary? Thank you.
import arcpy, csv
arcpy.env.overwriteOutput = True
#Set variables
arcpy.env.workspace = "C:\\GIS\\StackEx\\"
outFolder = arcpy.env.workspace
pointFC = "art2.shp"
coordSys = "C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems" + \
"\\Geographic Coordinate Systems\\World\\WGS 1984.prj"
csvFile = "C:\\GIS\\StackEx\\chicken.csv"
fieldName = "DATE1"
#Create shapefile and add field
arcpy.CreateFeatureclass_management(outFolder, pointFC, "POINT", "", "", "", coordSys)
arcpy.AddField_management(pointFC, fieldName, "TEXT","","", 10)
gpsTrack = open(csvFile, "r")
headerLine = gpsTrack.readline()
#print headerLine
#I updated valueList to remove the '\n'
valueList = headerLine.strip().split(",")
print valueList
latValueIndex = valueList.index("LAT")
lonValueIndex = valueList.index("LON")
dateValueIndex = valueList.index("DATE")
# Read each line in csv file
cursor = arcpy.InsertCursor(pointFC)
for point in gpsTrack.readlines():
segmentedPoint = point.split(",")
# Get the lat/lon values of the current reading
latValue = segmentedPoint[latValueIndex]
lonValue = segmentedPoint[lonValueIndex]
dateValue = segmentedPoint[dateValueIndex]
vertex = arcpy.CreateObject("Point")
vertex.X = lonValue
vertex.Y = latValue
feature = cursor.newRow()
feature.shape = vertex
feature.DATE1 = dateValue
cursor.insertRow(feature)
del cursor
gpsTrack = (csvFile, "r")appears to be an editing typo, as that would not have the desired result :) – blah238 Dec 6 '11 at 4:48split(). This is one of the things thecsvmodule was designed to account for. So when you useindex()to look forLON, it's not finding it because what you should really be looking for isLON\r\nor similar, which is of course unintuitive. – blah238 Dec 6 '11 at 5:26