How do i represent the degree (°) & Minute (') symbols in my Python Script

4363
22
Jump to solution
10-01-2013 05:43 PM
OLANIYANOLAKUNLE
Occasional Contributor II
I tried to populate a field by using the line below in my script but I got the error message below. Please how can I represent the degree (°) & Minute (') symbols in my Python Script. Any suggestions?


arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear",'[NUM1] & "°" & [NUM2]',"VB") File "C:\Users\Administrator\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{7F12A0CE-D617-8330-5EBC-E045415D9345}\PlanTool_addin.py", line 105 SyntaxError: Non-ASCII character '\xb0'
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Well this one you should be figuring out, since it tells you in the error what the problem is this time.  You missed the underscore in "PYTHON_9.3" (You wrote "PYTHON 9.3")

exp = 'str(!NUM1!) + "\xb0" + str(!NUM2!)' arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear", exp,"PYTHON_9.3")


If you want a space between the numbers and the minutes symbol after the second number, the expression should be:

exp = 'str(!NUM1!) + "\xb0 " + str(!NUM2!) + "'"'


If I had done this in Model Builder and exported it to Python like I normally do this thread would not be up to 16 posts.  That is how I do it normally.

Here is from the Model Builder export of a calculation that worked.

# Process: Calculate Field arcpy.CalculateField_management("GISMainFabric_Line_Clip_0", "Bear", "str( !NUM1! ) + \"\\xb0 \" + str( !NUM2! ) + \"'\"", "PYTHON_9.3", "")

View solution in original post

0 Kudos
22 Replies
ChrisPedrezuela
Occasional Contributor III
try:
arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear",str[NUM1] + '\xb0' + str[NUM2] + "'","PYTHON 9.3")

I tried to populate a field by using the line below in my script but I got the error message below. Please how can I represent the degree (°) & Minute (') symbols in my Python Script. Any suggestions?


arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear",'[NUM1] & "°" & [NUM2]',"VB")
File "C:\Users\Administrator\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{7F12A0CE-D617-8330-5EBC-E045415D9345}\PlanTool_addin.py", line 105 SyntaxError: Non-ASCII character '\xb0'
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
I tried your suggestion and this was what I got below:


Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{7F12A0CE-D617-8330-5EBC-E045415D9345}\PlanTool_addin.py", line 105, in onSelChange arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear",str[NUM1] + '\xb0' + str[NUM2] + "'","PYTHON 9.3")
NameError: global name 'NUM1' is not defined
0 Kudos
RichardFairhurst
MVP Honored Contributor
try:
arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear",str([NUM1]) + '\xb0' + str([NUM2]) + "'","PYTHON 9.3")



He forgot the parentheses for the str method.  Above revision should work.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Another error message

arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear",str([NUM1]) + '\xb0' + str([NUM2]) + "'","PYTHON 9.3")
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'NUM1' is not defined
0 Kudos
T__WayneWhitley
Frequent Contributor
Minor omission, I think you forgot the preceding quote on the expression -- that was drilled into me yesterday as I was forgetting it over and over again...

"'" + str([NUM1]) + '\xb0' + str([NUM2]) + "'"
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Still the same error message

("GISMainFabric_Line_Clip_0","Bear","'" + str([NUM1]) + '\xb0' + str([NUM2]) + "'" ,"PYTHON 9.3")
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'NUM1' is not defined
0 Kudos
T__WayneWhitley
Frequent Contributor
...uh, one more thing, I'm not sure but since it is a Python expression do you need the field delimiters (exclamation points) as shown?:

"'" + str(!NUM1!) + '\xb0' + str(!NUM2!) + "'"
0 Kudos
T__WayneWhitley
Frequent Contributor
OK, finally...

exp = 'str(!NUM1!) + \'\xb0\'' + str(!NUM2!)'
arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear", exp,"PYTHON 9.3")


...to further explain the expression (exp) I think denoting the fields with ! is pretty much self-explanatory - escaping the inner quotes was a little tricky.

You need to pass \xb0 for the degree symbol but you need to concatenate this inner string with the 2 outer field values (converted to string of course).
But you need to pass the quote as a quote, not to denote the end of the expression component, so \' does that... the outer one \'' interprets the 1st single quote as part of the string and the 2nd single quote denotes the end of the middle component - if in doubt, define exp as I have done above, and print it out...if say NUM1 equals 25 and NUM2 equals 50 (and let's just say the asterisk is the deg symbol - I forgot if this txt editor will properly interpret a deg symbol), then should be interpeted as:

25*50

...let's just say further that you'd like some 'whitespace' after the symbol, then in similar fashion do something like this, adding :

exp = 'str(!NUM1!) + \'\xb0  \'' + str(!NUM2!)'
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Im afraid this is what I got

exp = 'str(!NUM1!) + \'\xb0\'' + str(!NUM2!)'
Parsing error SyntaxError: invalid syntax (line 1)
0 Kudos