Skip to main content
 首页 » 编程设计

python之Unicode 与 Unicode 表示

2024年10月25日12飞鱼

对于 unicode 字符和该字符的表示之间的区别,我有点困惑。两者有什么区别:

>>> u'é' 
>>> u'\xe9' 

此外,我如何才能在“可读”unicode (é) 和机器 unicode(\xe9) 之间来回切换?我将如何针对以下情况执行此操作?

>>> u='bj\\u00f6rk: voltaic' 
>>> print u 
bj\u00f6rk: voltaic 

请您参考如下方法:

您想使用 repr :

print repr(u) 

print u'é' 在 shell 中为您提供 str 输出,在 shell 中 u'é' 向您显示 repr 输出。

str

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.