How do I modify my syntax to properly read an csv file and write the contents to a shapefile using an update cursor ?
# Read csv file & write to shapefile via update cursor
import csv,os
updateCursor = arcpy.da.UpdateCursor(r'sample.shp', ["Name", "OrderCount","Time","Miles"])
with open(r'CsvToShape.csv','rb') as f:
reader = csv.reader(f)
for row in reader:
route = row[0]
order = row[1]
time = row[2]
mile = row[3]
for row in updateCursor:
row.setValue(route = Name)
## row[1]= order
## row[2] = time
## row[3] = mile
updateCursor.updateRow(row)
You're using 'row' twice, for different objects. This cannot result in success.
You're also attempting to use setValue on an array, which is wrong (DA cursors are very different creatures the the old , deprecated cursors)
The canonical approach to this problem is to read the CVS once, into a dictionary by key column, then to loop the update cursor once, consulting the dictionary. Double-nested looping with cursors is exceeding slow, and should be avoided.
- V
I am trying to find solution to CSV file that automatically become numeric . When i finish my survey I export it to CSV file and I am using RAnalitic software where i need to convert text to number as mentioned yes or no text should be 1 or 2 and so on. Is there any function i can use to do this?
This is a different question, but no, there isn't a function to make arbitrary text into integer.
It is, however, simple to use a dictionary to assign an numeric lookup value for text values if you create a dictionary that performs the encoding for you:
lookup1 = { 'Yes' : 1, 'No' : 0, 'DEFAULT' : -1 }
for txt in ['Yes','No','YES','NO','Maybe']:
alt = -1
if (txt in lookup1):
alt = lookup1[txt]
#else:
# alt = lookup['DEFAULT']
print("{:>10s} : {:d}".format(txt,alt))