0

I got a tiny script in ArcGIS which creates a hyperlink.

My code:

def Befahrung(value1, value2):
    if value1 is '':
        return ''
    else:
        return "G:\\Example\\" + str(value1) + "\\File_" + str(value2) + ".pdf"

The error (only when !Bezeichnun! contains a special character):

ERROR 000539: Error running expression: Befahrung(u" ",u"1155Mönch1")
Traceback (most recent call last):
  File "<expression>", line 1 in <module>
  File "<string>", line 5 in Befahrung
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 5: ordinal not in range(128)

!Bezeichnun! and !Auftrag! are both strings. It works very well until !Bezeichnun! contains a special character. I can't change the characters, I need to save them.

What do I have to change?

jww
  • 97,681
  • 90
  • 411
  • 885
GEOGEO
  • 1
  • 1
  • possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – Cory Kramer Aug 22 '14 at 11:37
  • You may should consider gis.stackexchange for that type of question – four-eyes Aug 22 '14 at 12:11
  • gis.stackexchange told me to consider stackoverflow – GEOGEO Aug 22 '14 at 12:17
  • Dropping the hot potato – Steve K Aug 22 '14 at 12:45
  • 1
    @Stophface Troubleshooting at GIS SE showed that this happens even when ArcGIS, its Field Calculator and ArcPy are not in the loop: http://gis.stackexchange.com/questions/111416/python-ascii-codec-synthax-question-in-field-calculator I think the asker should post their pure Python/IDLE test code here to illustrate that. – PolyGeo Aug 22 '14 at 22:56
  • 1
    Make sure you use `if value1 == ''` instead of `if value1 is ''`. ArcGIS stores everything as unicode, and `u'' is ''` returns `False`. – nmpeterson Aug 23 '14 at 01:51

2 Answers2

2

In Befahrung, you convert a string (Unicode in this case) to ASCII:

str(value1);
str(value2);

cannot work if value1 or value2 contain non-ASCII characters. You want to use

unicode(value1)

or better, use string formatting:

return u"G:\\Example\\{}\\File_{}.pdf".format(value1, value2)

(works in Python 2.7 and above)

Steve K
  • 10,879
  • 4
  • 39
  • 39
  • If i try to use string formatting i get: ERROR 000539: SyntaxError: EOL while scanning string literal (, line 1) Fehler beim Ausführen von (Feld füllen mit Verlinkung). – GEOGEO Aug 22 '14 at 12:06
  • I assume you correctly used the double quotes around your format string ? That is `u"..."` and not `u"...''` for example. The error says your code contains a string which is not ended. (eg. `"hello`) – Steve K Aug 22 '14 at 12:13
  • Wow, that's strange. This works in a Python 2.7 interpreter (that is formatting with Unicode arguments). – Steve K Aug 22 '14 at 12:43
  • @SteveK As far as I know the ArcGIS Field Calculator is not quite a "pure" Python interpreter so there is a slight possibility that something that works in a Python 2.7 interpreter could throw an error there but at [GIS Stack Exchange](http://gis.stackexchange.com/questions/111416/python-ascii-codec-synthax-question-in-field-calculator) we asked if the error was showing up in IDLE too and the answer was "yes". – PolyGeo Aug 22 '14 at 23:05
  • 1
    Does it still not work if you use `u"G:\\Example\\{0}\\File_{1}.pdf".format(value1, value2)` (note the `0` and `1`) and the "PYTHON_9.3" interpreter? – nmpeterson Aug 23 '14 at 01:46
2

I recommend reading the Python Unicode HOWTO. The error can be distilled to

>>> str(u"1155Mönch1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 5: ordinal not in range(128)

If you know what character encoding you need (e.g., UTF-8), you can encode it like

value1.encode('utf-8')
Anton
  • 4,411
  • 1
  • 15
  • 18