Difference between decoding and unicode?

According to this test:

# -*- 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)

There is no difference between .decode()and unicode():

aaaaa          5 <type 'unicode'> aaaaa          5 <type 'unicode'>       True
ááááá          5 <type 'unicode'> ááááá          5 <type 'unicode'>       True
ℕℤℚℝℂ          5 <type 'unicode'> ℕℤℚℝℂ          5 <type 'unicode'>       True

I'm right? If so, why do we have two different ways to solve the same thing? Which one should I use? Is there any subtle difference?

+4
source share
3 answers

Comparing the documentation for the two functions ( here and here ), the differences between the two methods seem very slight. The function is unicodedocumented as

/ , unicode() 8- , , . encoding ; , LookupError . ; , . "" ( ), ValueError ,...

string.decode

, . . . - "", , UnicodeError....

, , -, , unicode , , (ValueError vs. UnicodeError). , : unicode " 2.0", string.decode - " 2.2".

, , , .

+6

, , str, Unicode, . :

  • u.decode(e) - , , unicode(s,e).

  • , / Unicode, base64 string-escape, encode()/decode(), unicode(). ( , , Unicode, , Python 3 , , .)

  • unicode(), , str, Unicode .

0

All Articles