[GIS] ArcPy Field Calculation with special characters

arcgis-desktoparcpyfield-calculator

I'm trying to calculate a string field, using "µSv/h". Running this in the manual field calculator works just fine, but I've tried this in the Python window in ArcMap as well as the IDLE, but both fail. For the line below it says invalid syntax.

arcpy.CalculateField_management(tblTempAssayImportName,"Dose_units",""""µSv/h"""","PYTHON_9.3")

When I try to remove the additional three quotes, it comes up with a 000539 error.

I've tried using r to escape, The line above comes straight from the snippet after running the field calculator manually. Not sure why this isn't working, but I'm assuming it's the special characters.

Best Answer

I'm no expert with character encoding, but I believe Python's interpreter converts your input source to ASCII which is why using triple quotes and the r string prefix aren't having any effect.

What you need to do is use Python's Unicode escape characters and the hexadecimal code for mu, as described in the Unicode HOWTO page. According to the Unicode table, the hexadecimal code for lowercase mu is 03BC. To create a Unicode string in Python for lowercase mu, you would enter u'\u03BC'. Your full field calculator expression would be:

arcpy.CalculateField_management(tblTempAssayImportName,"Dose_units",u"\u03BCSv/h","PYTHON_9.3")
Related Question