Uma
Uma

Reputation: 1

Conversion of Unicode to List type in python

The below one is working properly,

>>> a = 1,2
>>> a = u'[1,2]'
>>> print a
[1,2]
>>> type(a)
<type 'unicode'>
>>> n = [e.encode('utf-8') for e in a.strip('[]').split(',')]
>>> n
['1', '2']
>>> type(n)
<type 'list'>
#

But when include it in the program. Instead of changing the value from unicode to list, its just changing the variable name.

>>> a = 1,2
>>> a = u'[a]'
>>> print a
[a]  # Instead of 1,2.

Please help me on this.,

Upvotes: 0

Views: 714

Answers (3)

Lennart Regebro
Lennart Regebro

Reputation: 172279

This does what you apparently are trying to do:

>>> a = 1,2
>>> print a
1,2

What you really need to do is beyond me. There is no practical reason to convert a list to unicode, really.

Upvotes: 0

joaquin
joaquin

Reputation: 85603

when you make a = u'[a]' you are not using the initial variable a put inside a list but the characters '[a]'

>> a = 1,2
>> a = u'[what?]'
>> print a
[what?]

Upvotes: 2

Senthil Kumaran
Senthil Kumaran

Reputation: 56891

I think, what you are looking for is unicode function

>>> a = 1,2
>>> a
(1, 2)
>>> unicode(a)
u'(1, 2)'
>>> a = [1,2]
>>> unicode(a)
u'[1, 2]'
>>> 

Upvotes: 1

Related Questions