RaviKiran
RaviKiran

Reputation: 803

How to remove ('u')unicode from list of dictionaries?

I have a list of dicts like this:

d = [{'email': u'[email protected]', 'name': u'xxx'}, {'email': u'[email protected]', 'name': u'xxx1'}, {'email': u'[email protected]', 'name': u'xxx2'}]

I want that unicode character to be removed, so my output should look like

d = [{'email': '[email protected]', 'name': 'xxx'}, {'email': '[email protected]', 'name': 'xxx1'}, {'email': '[email protected]', 'name': 'xxx2'}]

can anyone please tell me?

Upvotes: 2

Views: 1688

Answers (1)

DanGar
DanGar

Reputation: 3078

You can also encode you unicode characters in utf-8 like:

In [2]: d = [{'email': u'[email protected]', 'name': u'xxx'}, {'email': u'[email protected]', 'name': u'xxx1'}, {'email': u'[email protected]', 'name': u'xxx2'}]

In [3]: new_d = [{k: v.encode("utf-8") for k, v in elem.items()} for elem in d]
In [4]: new_d
Out[4]: 
[{'email': '[email protected]', 'name': 'xxx'},
 {'email': '[email protected]', 'name': 'xxx1'},
 {'email': '[email protected]', 'name': 'xxx2'}]

Upvotes: 3

Related Questions