简体   繁体   English

ArcGIS Field Calculator中的语法错误

[英]Syntax Error in ArcGIS Field Calculator

I got a tiny script in ArcGIS which creates a hyperlink. 我在ArcGIS中有一个小脚本,可以创建超链接。

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): 错误(仅当!Bezeichnun!包含特殊字符时):

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! !Auftrag! are both strings. 都是字符串。 It works very well until !Bezeichnun! 直到!Bezeichnun!都很!Bezeichnun! contains a special character. 包含一个特殊字符。 I can't change the characters, I need to save them. 我无法更改字符,我需要保存它们。

What do I have to change? 我必须改变什么?

In Befahrung , you convert a string (Unicode in this case) to ASCII: Befahrung ,将字符串(在这种情况下为Unicode)转换为ASCII:

str(value1);
str(value2);

cannot work if value1 or value2 contain non-ASCII characters. 如果value1value2包含非ASCII字符,则无法使用。 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) (适用于Python 2.7及更高版本)

I recommend reading the Python Unicode HOWTO . 我建议阅读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 (eg, UTF-8), you can encode it like 如果知道所需的字符编码(例如UTF-8),则可以像

value1.encode('utf-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM