Reputation: 15000
I have a list like this
d=[{u'Length': u'2.96m', u'Width': u'1.44m', u'height': u'0.38m'},
{u'Length': u'3.8m', u'Width': u'0.65m', u'height': u'9.3m'},
{u'Length': u'0.62m', u'Width': u'2.9m', u'height': u'3.5m'}]
and I want a simple way to remove unicode 'u' from this list to create a new list. The "simple way" here I ment to remove unicode without importing an external module or saving it in external files.
These are the five methods I tried
def to_utf8(d):
if type(d) is dict:
result = {}
for key, value in d.items():
result[to_utf8(key)] = to_utf8(value)
elif type(d) is unicode:
return d.encode('utf8')
else:
return d
#these three returns AttributeError: 'list' object has no attribute 'encode'
d.encode('utf-8')
d.encode('ascii')
d.encode("ascii","replace")
#output the same
to_utf8(d)
print str(d)
firs three returns
AttributeError: 'list' object has no attribute 'encode'
and last two prints the same result. How should I remove unicode 'u'?
Upvotes: 3
Views: 7274
Reputation: 550
How about this, iterate list and encode each key, value in dictionary.
converted = [{ str(key): str(value)
for key, value in array.items()
} for array in d]
print (converted)
Upvotes: 3
Reputation: 2489
This is the simplest solution
d=[{u'Length': u'2.96m', u'Width': u'1.44m', u'height': u'0.38m'},
{u'Length': u'3.8m', u'Width': u'0.65m', u'height': u'9.3m'},
{u'Length': u'0.62m', u'Width': u'2.9m', u'height': u'3.5m'}]
def to_utf8(d):
final = []
for item in d:
if type(item) is dict:
result = {}
for key, value in item.items():
result[str(key)] = str(value)
final.append(result)
return final
print to_utf8(d)
Upvotes: 2
Reputation: 6475
You should encode them to bytes first and decode them to ascii string.
l = list()
for item in d:
temp = dict()
for key, value in item.items():
temp[key.encode("utf-8").decode("ascii")] = value.encode("utf-8").decode("ascii")
l.append(temp)
Upvotes: 0