5
# -*- coding: utf-8 -*-

a = 'éáűőúöüó€'
print type(a)    # <type 'str'>
print a          # éáűőúöüó€
print ord(a[-1]) # 172

为什么这行得通?不应该是这个SyntaxError: Non-ASCII character '\xc3' in file ...吗?字符串中有 unicode 文字。

当我在它前面加上前缀时u,结果会有所不同:

# -*- coding: utf-8 -*-

a = u'éáűőúöüó€'
print type(a)    # <type 'unicode'>
print a          # éáűőúöüó€
print ord(a[-1]) # 8364

为什么?python中的内部表示有什么区别?我自己怎么看?:)

4

1 回答 1

10
于 2013-02-12T18:19:51.567 回答