Select to view content in your preferred language

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

5557
22
Jump to solution
10-01-2013 05:43 PM
OLANIYANOLAKUNLE
Frequent Contributor
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
22 Replies
MattSayler
Frequent Contributor
Whole expression has to evaluate to a string. It's still thinking the fields in the expressions are variables. Try:

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


screenshot:

[ATTACH=CONFIG]27969[/ATTACH]
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks, but this is what I got:

exp = 'str(!NUM1!) + "\xb0" + str(!NUM2!)'
>>> arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear", exp,"PYTHON 9.3")
... 
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 3128, in CalculateField     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000800: The value is not a member of VB | PYTHON | PYTHON_9.3. Failed to execute (CalculateField).
0 Kudos
RichardFairhurst
MVP Honored Contributor
Thanks, but this is what I got:

exp = 'str(!NUM1!) + "\xb0" + str(!NUM2!)'
>>> arcpy.CalculateField_management("GISMainFabric_Line_Clip_0","Bear", exp,"PYTHON 9.3")
... 
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 3128, in CalculateField     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000800: The value is not a member of VB | PYTHON | PYTHON_9.3. Failed to execute (CalculateField).


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!) + "'"'
0 Kudos
MattSayler
Frequent Contributor
Try "PYTHON_9.3" instead of "PYTHON 9.3"
0 Kudos
OLANIYANOLAKUNLE
Frequent 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!) + "'"'


This error below was what I got when I tried your suggestion,

>>> exp = 'str(!NUM1!) + "\xb0 " + str(!NUM2!) + "'"' 
Parsing error SyntaxError: EOL while scanning string literal (line 1)
0 Kudos
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", "")
0 Kudos
T__WayneWhitley
Honored Contributor
oops, I had too many quotes... again the \' means you are passing the quote itself as text, so the extra one is going to fail.

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

...should be:

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


This time I made the text color red for clarity. Sorry for the mishap, and also I told you to print out the exp as a statement so you can 'see' what is passed when you use it for the field calculator.

Enjoy,
Wayne
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
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", "")


Thanks rfairhur24 It Worked perfectly!!!
0 Kudos
MattSayler
Frequent Contributor
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", "")


Harder to read though. I think you had it before, just needed to escape the ':
exp = 'str(!NUM1!) + "\xb0 " + str(!NUM2!) + "\'"'


The whole deal is kind of confusing because you have to write code that has to evaluate to a string that has to evaluate to code.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Harder to read though. I think you had it before, just needed to escape the ':
exp = 'str(!NUM1!) + "\xb0 " + str(!NUM2!) + "\'"'


The whole deal is kind of confusing because you have to write code that has to evaluate to a string that has to evaluate to code.


I agree that the fix you suggest would work and is more readable.  I just prefer to use Model Builder to write the expression, because then I just have to write the equation, which is easier to read than anything I deal with in a Python script.  To get my output I just had to know how to write the expression:

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

Much easier to get correct than any of the python conversions to a string.
0 Kudos