【发布时间】:2014-01-06 12:14:57
【问题描述】:
根据这个测试:
# -*- coding: utf-8 -*-
ENCODING = 'utf-8'
# what is the difference between decode and unicode?
test_cases = [
'aaaaa',
'ááááá',
'ℕℤℚℝℂ',
]
FORMAT = '%-10s %5d %-10s %-10s %5d %-10s %10s'
for text in test_cases :
decoded = text.decode(ENCODING)
unicoded = unicode(text, ENCODING)
equal = decoded == unicoded
print FORMAT % (decoded, len(decoded), type(decoded), unicoded, len(unicoded), type(unicoded), equal)
.decode()和unicode()没有区别:
aaaaa 5 <type 'unicode'> aaaaa 5 <type 'unicode'> True
ááááá 5 <type 'unicode'> ááááá 5 <type 'unicode'> True
ℕℤℚℝℂ 5 <type 'unicode'> ℕℤℚℝℂ 5 <type 'unicode'> True
我说的对吗?如果是这样,为什么我们有两种不同的方式来完成同一件事?我应该使用哪一个?有什么细微的差别吗?
【问题讨论】:
标签: python python-2.7 unicode