Question

I have a list of dicts like this:

d = [{'email': u'123@gmail.com', 'name': u'xxx'}, {'email': u'abc@gmail.com', 'name': u'xxx1'}, {'email': u'xyz@gmail.com', 'name': u'xxx2'}]

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

d = [{'email': '123@gmail.com', 'name': 'xxx'}, {'email': 'abc@gmail.com', 'name': 'xxx1'}, {'email': 'xyz@gmail.com', 'name': 'xxx2'}]

can anyone please tell me?

Was it helpful?

Solution

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

In [2]: d = [{'email': u'123@gmail.com', 'name': u'xxx'}, {'email': u'abc@gmail.com', 'name': u'xxx1'}, {'email': u'xyz@gmail.com', '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': '123@gmail.com', 'name': 'xxx'},
 {'email': 'abc@gmail.com', 'name': 'xxx1'},
 {'email': 'xyz@gmail.com', 'name': 'xxx2'}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top