ArcPy Field Calculation with special characters


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.

2 Responses to “ArcPy Field Calculation with special characters”

  1. Fezter says:

    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")
    
  2. MierMoto says:

    Seems like I needed to escape the text with different quotes and then add one set for the text to be sent to the calculator. In this case I used three sets of single quotes and one set of double. This also escaped the special characters.

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

Leave a Reply

You must be logged in to post a comment.

Question and Answer is proudly powered by WordPress.
Theme "The Fundamentals of Graphic Design" by Arjuna
Icons by FamFamFam