newbie question about unicode

G

Genie T

Hi,

can anybody tell me whether these two expressions have the same
meanings?

s = u'<unicode string here>'
s1 = s.encode('utf-8')

AND

s1 = unicode(s,'utf-8')

Thanks :)
 
C

Carsten Haese

Hi,

can anybody tell me whether these two expressions have the same
meanings?

s = u'<unicode string here>'
s1 = s.encode('utf-8')

AND

s1 = unicode(s,'utf-8')

Considering that one works and the other doesn't, no, they don't have the same
meaning.

The unicode() function decodes a given byte string into a unicode object, but
you're giving it a unicode object. What are you actually trying to achieve?

If you're just trying to get a handle on the topic, I recommend you read
http://www.amk.ca/python/howto/unicode

HTH,
 
G

Gabriel Genellina

can anybody tell me whether these two expressions have the same
meanings?

s = u'<unicode string here>'
s1 = s.encode('utf-8')

AND

s1 = unicode(s,'utf-8')

No - but consider this (assuming your terminal uses utf-8):

py> u1 = u'<some text here>'
py> s1 = u1.encode('utf-8')
py>
py> s2 = '<some text here>'
py> u2 = s2.decode('utf-8')
py>
py> type(u1), type(u2)
(<type 'unicode'>, <type 'unicode'>)
py> u1==u2
True
py> type(s1), type(s2)
(<type 'str'>, <type 'str'>)
py> s1==s2
True
 
G

Genie T

On Sat, 23 Jun 2007 04:10:19 -0000, Genie T wrote




Considering that one works and the other doesn't, no, they don't have the same
meaning.

The unicode() function decodes a given byte string into a unicode object, but
you're giving it a unicode object. What are you actually trying to achieve?

If you're just trying to get a handle on the topic, I recommend you readhttp://www.amk.ca/python/howto/unicode

HTH,

thanks for the link, i'm trying to understand more about this unicode
handling :)
 
G

Genie T

No - but consider this (assuming your terminal uses utf-8):

py> u1 = u'<some text here>'
py> s1 = u1.encode('utf-8')
py>
py> s2 = '<some text here>'
py> u2 = s2.decode('utf-8')
py>
py> type(u1), type(u2)
(<type 'unicode'>, <type 'unicode'>)
py> u1==u2
True
py> type(s1), type(s2)
(<type 'str'>, <type 'str'>)
py> s1==s2
True

Thanks, i'm clear now
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top