Select to view content in your preferred language

Can't update feature class from csv file in python script

419
4
2 weeks ago
GeorgeJonesRIGOV
Regular Contributor

hi, all I've been trying to run a script to update a feature class but the message i get (part of the script) state "No matching values in CSV of join field: plus the value ". any advice? this is the script i have.  thanks 

 

import arcpy
import csv
gdb_path = r'S:\PW\GIS\Map Requests\Water_MeterProject\MapProject_11x17\RIDVSQL.sde'
fc = r'S:\PW\GIS\Map Requests\Water_MeterProject\MapProject_11x17\RIDVSQL.sde\RIGIS.DBO.Water_Features\RIGIS.DBO.ResidentialWaterMeter'
csv_file = r'S:\PW\GIS\Map Requests\Water_MeterProject\Account Data 100124.csv'

#fields in fc to update
fc_fields = ['Address', 'SerialNumber']
#field in csv to update
csv_fields = ['service_address', 'serial_no']

csv_data = {}
with open(csv_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
csv_data[row[csv_fields[0]]] = row
arcpy.env.workspace = gdb_path
edit = arcpy.da.Editor(gdb_path) #start edit session
edit.startEditing(False, True)
edit.startOperation()

with arcpy.da.UpdateCursor(fc, fc_fields) as cursor:
for row in cursor:
join_value = row[0]
if str(join_value) in csv_data:
csv_row = csv_data[str(join_value)]
for i in range(1, len(fc_fields)):
fc_field_name = fc_fields[i]
csv_field_name = csv_fields[i]
row[i] = csv_row[csv_field_name]
cursor.updateRow(row)
else:
print("No matching values in CSV of join field: {join_value}")
edit.stopOperation()
edit.stopEditing()
print("updatedSucessfully")

0 Kudos
4 Replies
CodyPatterson
MVP Regular Contributor

Hey @GeorgeJonesRIGOV 

Would it be possible to print both the join_value and the csv_data? It could be that the join_value is not in the csv_data, the address could be null or none as well.

I would start placing some prints around that would allow for some debugging!

Cody

DanPatterson
MVP Esteemed Contributor

to format the code with line numbers for reference, see...

Code formatting ... the Community Version - Esri Community


... sort of retired...
GeorgeJonesRIGOV
Regular Contributor

It should just match. Have the exact values in both the CSV and feature class .

0 Kudos
DavidPike
MVP Notable Contributor

The error is just printed from your Else statement so I wouldn't take it as wrote.  If you can format your code with indentation that would help troubleshoot.  Dan has linked the formatting guide above.

0 Kudos