Skip to main content
 首页 » 编程设计

python之在 python 中编码 : what type is the variable

2025年05月04日10Leo_wl

Python文件

# -*- coding: UTF-8 -*- 
a = 'Köppler' 
print a 
print a.__class__.__name__ 
mydict = {} 
mydict['name'] = a 
print mydict 
print mydict['name'] 

输出:

Köppler 
str 
{'name': 'K\xc3\xb6ppler'} 
Köppler 

名称似乎保持不变,但只有在打印字典时我才会得到这个奇怪的转义字符串。那我在看什么?那是 UTF-8 表示吗?

请您参考如下方法:

该行为的原因是 Python 2 中的 __repr__ 函数 escapes non-ASCII unicode characters .如链接所示,这已在 Python 3 中修复。