I have a number of feature classes in which some records in the attribute tables need updating with new information. However the strings to replace aren't found in the UpdateCursor due to the presence of accented characters. I've run the same search on the table and just removed the accented e (é) from both the record and the search string and the string is found and replaced correctly.
# For each row, evaluate the PRESENCE field (index position
# of 0),
# and update with new value if the record reads
# "Présence n'est pas probable"
with arcpy.da.UpdateCursor(fc, inputField) as cursor:
for row in cursor:
if row[0] == "Présence n'est pas probable":
row[0] = "Non probable"
cursor.updateRow(row)
else:
pass
How does one search for a string with accented characters?
-
1Saw other post: stackoverflow.com/questions/13325753/…enolan– enolan2018年07月25日 17:06:47 +00:00Commented Jul 25, 2018 at 17:06
-
Also this with regex which should work: stackoverflow.com/questions/50847976/…enolan– enolan2018年07月25日 17:07:40 +00:00Commented Jul 25, 2018 at 17:07
1 Answer 1
As commented you can remove the accents using unicodedata.normalize. I have not tried the code but I think it should work:
import unicodedata, arcpy
def remove_accents(input_str):
#From: https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string
nfkd_form = unicodedata.normalize('NFKD', input_str)
only_ascii = nfkd_form.encode('ASCII', 'ignore')
return only_ascii
with arcpy.da.UpdateCursor(fc, inputField) as cursor:
for row in cursor:
if remove_accents(row[0]) == remove_accents("Présence n'est pas probable"):
row[0] = "Non probable"
cursor.updateRow(row)
else:
pass