CalculateField_management problem setting string data

117
1
02-21-2019 01:30 PM
RobertHarvey
New Contributor

I am reading in ExtendedData/Data from a KML to add additional data fields/data to a FeatureClass using ArcPRO 2.0.1. I am using a hashmap to store the key-value Data/value pairs. To set the data, I am making calls with arcpy.CalculateField_management. When I grab the data, I am prepending and appending double quotes " (ie. hashData = '"' + hashKMLData[hashKey] + "'" and thus making the call:

arcpy.CalculateField_management(myfeature, hashKey, hashData)

Some of the data are just numbers and ARE stored correctly. However, Strings/String mixed with numbers and List data (enclosed in '[ ]' brackets) are problematic. Only the first character is getting set in the corresponding fields. I have tried to coerce the data types based on the field types (ie. str(hashData)). I have even tried setting all fields to "Text" but still have troubles with string data (ie. "I say foo a lot" gets stored as "I" in our SDE). It's as though setting these values is truncated to the first character.

I'm at a loss - any help is much appreciated.

Thanks

0 Kudos
1 Reply
curtvprice
MVP Esteemed Contributor

In general it's much easier to use Python string formatting:

# Instead of:# hashData = '"' + hashKMLData[hashKey] + "'"  
# arcpy.CalculateField_management(myfeature, hashKey, hashData)
# try this:
hashData = '"{}"'.format(hashKMLData[hashkey])

#debug using repr to see what's REALLY there
print(repr(hashkey))
print(repr(hashKMLData[hashkey]))
print(repr(hashData))

arcpy.CalculateField_management(myfeature, hashKey, hashData)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

But I think the issue is your data are not ready to be stringed, like there are embedded lists which you seem to be saying here. This means you need to do some additional debugging and coding, but it's a LOT easier to do it using string formatting instead of concatenating string with +.

Playing with parsing your data at a python prompt >>> can be often be valuable when figuring out strategies to handle your data.

Keep in mind that repr(hashData) should look like this when passed to Calculate Field as a calculate expression:

r"'My favorite string'" # strings, with any embedded quotes escaped
r"999.0" or 999.0       # numbers‍‍