Converting unicode list to string of list
20,109
Solution 1
This will return d
as an array of arrays with ascii strings instead of unicode.
# Iterate through each list in listoflist
# Then iterate through each unicode string in listoflist
d = [[s.encode('ascii') for s in list] for list in listoflist]
Also as @pm-2ring mentioned, you can also use s.encode('ascii', 'ignore')
if you would like to ignore unicode
strings that cannot be converted to ascii
.
To get each list we use. for list in listoflist
.
To get each unicode string we use. for s in list
.
Then to convert we use s.encode('ascii')
Solution 2
If you want to make your code understandable, do this
for l in listoflist:
d_temp = []
for s in l:
d_temp.append(s.encode('ascii'))
d.append(d_temp)
Comments
-
KevinOelen over 1 year
I have a list of list unicode. And now I need to convert it to list of list string. How can I do that?
listoflist = [ [ u'keep', u'see', u'recover', u'try', u'cry', u'say', u'seem', u'come', u'saw', u'have', u'be', u'begin', u'fell', u'wait', u'come', u'wait', u'be', u'retire', u'be' ], [ u'make', u'let', u'forget', u'forgive', u'punish', u'take', u'be', u'take', u'forget', u'come', u'think', u'say', u'be', u'be', u'say', u'think', u'jump', u'poke', u'come', u'be', u'have', u'try', u'come', u'turn', u'approach', u'be', u'meet', u'try', u'run', u'boast', u'bring', u'satisfy', u'use', u'be', u'leave', u'be', u'do', u'say', u'bristle' ] ]
I tried to use the
ast
libraryimport ast d = [] for i in range(0,50): d.append([item.encode('ascii') for item in ast.literal_eval(listoflist)])
But i get the following error.
raise ValueError('malformed string') ValueError: malformed string
Different approaches are welcome.