-2

Can someoene help me to convert unicode to list my unicode data looks like this

data=[u'1APT', u'CCS', u'COMRM']

data =type 'unicode'

And want to convert it to

data=['1APT', 'CCS','COMRM']

data = type 'list'

here are more details for my problem, here is code

genlist2= request.POST['data2']
print type(genlist2)
print "genlist2"
print genlist2
for d in genlist2:
    print d

Output:

type 'unicode'

[u'1APT', u'CCS', u'COMRM']

[

u

'

1

A

p

T

etc.. 

Why in output in for loop , I get every character separated , i want

1APT
CCS
COMRM
??
David.Zheng
  • 903
  • 8
  • 6
ivan
  • 29
  • 2
  • 10
  • 3
    You already have a list, a list of unicode strings.. – Inbar Rose Aug 20 '13 at 08:11
  • 1
    This syntax is invalid, so it's not clear what form your data actually takes. It looks like it's already a list. Could you use actual Python syntax, so we know what we're dealing with? – user2357112 Aug 20 '13 at 08:12

2 Answers2

3
>>> data = map(str, data)
>>> data
['1APT', 'CCS', 'COMRM']

But you may want to question your need for doing this.

Jared
  • 25,627
  • 7
  • 56
  • 61
0
>>>newdata = [i.encode() for i in data]
>>>print newdata
['1APT', 'CCS', 'COMRM']

EDIT:

I have understood what's your problem. genlist2 is a string, and its content is just like a python list.

genlist2 = "[u'1APT', u'CCS', u'COMRM']"

eval(genlist2) will return a list which is what you want. eval() is usually not safe and have some other problems, you can reference Is using eval in Python a bad practice? .

I don't know other ways to get what you want.

Community
  • 1
  • 1
David.Zheng
  • 903
  • 8
  • 6
  • this won't work for me , when I do it I get ['[', 'u', "'", '1', 'A', 'P', 'T', "'", ',', ' ', 'u', "'", 'C', 'C', 'S – ivan Aug 20 '13 at 08:38
  • I want as your print out ['1APT', 'CCS', 'COMRM'] – ivan Aug 20 '13 at 08:39
  • I will explain my problem in more detaisl , I have data structure [u'1APT', u'CCS', u'COMRM'] , that stores with vobs = request.POST['data2'] – ivan Aug 20 '13 at 08:40
  • when I run for data in vobs: print data – ivan Aug 20 '13 at 08:40
  • every character in each itereations are stored in data – ivan Aug 20 '13 at 08:41
  • I want in each iteration stored in data 1APT then CCS and etc. – ivan Aug 20 '13 at 08:42
  • when I received same variable with vobs request.session['vobs'] , it is type : list and all works normally – ivan Aug 20 '13 at 08:43
  • when I print request.session['vobs'] and request.POST['data2'] , I get same result , only different type request.session['vobs'] is list , request.POST['data2'] is unicode – ivan Aug 20 '13 at 08:44
  • I checked type with print type(vobs) – ivan Aug 20 '13 at 08:44
  • @ivan, I could not quite understand your question described here, could you add the description here in a well-formed format and update the problem above? – David.Zheng Aug 20 '13 at 09:40
  • please check now, I updated my question ... You should now understand my question and maybe help me to resolve this weird situtation – ivan Aug 20 '13 at 11:21