Parsing csv data and updating a dictionary

04-10-2019 06:52 PM
New Contributor

Hello All:

I am a little stuck on my code.  I am new to python and am learning as I go, but have usually been able to figure it out till now.  The code was working up till I put the dictionary part in and now this is where its failing.  When I run the program, it only goes through once and doesn't appear to be storing the data.  I have found people in the discussions have had the same problem, but they never said what helped them resolve the issue.  Below is a breakdown of the objective of the program:

The rhinos in the spreadsheet appear in no guaranteed order, and not all the rhinos appear at the beginning of the spreadsheet. As I parse each line, I must determine which rhino the reading belongs to and update that rhino's polyline track accordingly. I am not allowed to sort the Rhino column in Excel before I export to the CSV file. My script must be "smart" enough to work with an unsorted spreadsheet in the order that the records appear.
I do not immediately know how many rhinos are in the file or even what their names are. Although I could visually comb the spreadsheet for this information and hard-code each rhino's name, your script is required to handle all the rhino names programmatically. The idea is that I should be able to run this script on a different file, possibly containing more rhinos, without having to make many manual adjustments.

Sample Data:


Appreciate any help and guidance anyone has to offer.

#Import a csv file into ArcMap and plot the points for each Rhino
import arcpy
import csv

arcpy.env.workspace = "C:\\GEOG485\\Lesson4\\Project4"
arcpy.env.overwriteOutput = True

targetFolder = "C:\\GEOG485\\Lesson4\\Project4"
rhinoShape = "C:\\GEOG485\\Lesson4\\Project4\\Rhino.shp"
rhinoFile = "C:\\GEOG485\\Lesson4\\Project4\\RhinoObservations.csv"
spatialRef = arcpy.Describe(rhinoShape).spatialReference

#Open CSV file and read the headers   
trackPoints = open(rhinoFile, "r")    
textLine = trackPoints.readline()    
headerLine = textLine.strip("\n")     
pairList = headerLine.split(",")    
print pairList    

polylineArray = arcpy.Array()    

#Get index for header fields    
xIndex = pairList.index("X")    
yIndex = pairList.index("Y")    
rhinoIndex = pairList.index("Rhino")    
print rhinoIndex   

#Created an empty dictionary    
dictionaryRhinoTracks = {}        
for line in trackPoints.readlines():    
line = line.rstrip("\n")    
splitLine = line.split(",")    
print splitLine    

#Created variable for needed field items based on index location    
pointX = splitLine[xIndex]    
pointY = splitLine[yIndex]    
rhinoName = splitLine[rhinoIndex]     

#Create a point object     
currentPoint.x = pointX    
currentPoint.y = pointY    
currentPoint = arcpy.Point(currentPoint.x, currentPoint.y)    
print currentPoint   

#Check if the dictionary does not contain name    
if rhinoName not in dictionaryRhinoTracks:       
rhinoTrackArray = polylineArray.Add(currentPoint)       
#Add the point object to the Array       dictionaryRhinoTracks[rhinoName].Add(currentPoint)    

cursor = arcpy.da.InsertCursor(rhinoShape, ['SHAPE@'])    
for rhinoName in dictionaryRhinoTracks:        
row.Shape = dictionaryRhinoTracks[rhinoName]       
row.Rhino_Name = rhinoName       
except: print ("error again")‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Reply
MVP Emeritus

A couple house keeping items:  I suggest you get into the habit of using the syntax highlighter.  You can find it by clicking the three dots to expand the menu, click more, syntax highlighter and finally select python.  It just makes reading your code much easier.

You have a huge try/except block, with little to go on if you get a failure.  You might want to break up you various steps into separate try/except blocks, something like this:

import arcy, etc

   first step of the process
   blah blah bla
except Exception as err:
    print err

   second step of the process
   blah blah blah
except Exception as err:
   print err

except Exception as err:
   print err

That should just about do it....
0 Kudos