What are you iterating for? You just need to replace the character from the string using str.replace()
.
output['title'] = output['title'].replace("\u200c", "")
This only changes value of the 'title'
key of output
{'title': 'title 1', 'subject': 'subject1\u200c'}
If you want to remove the character from all items in output
, you need a loop.:
for key, value in output.items():
output[key] = value.replace("\u200c", "")
Or, as a dict comprehension:
output = {key: value.replace("\u200c", "") for key, value in output.items()}
{'title': 'title 1', 'subject': 'subject1'}
Addressing your comments
I got this error for part one list indices must be integers or slices, not str
I got this error for second answer: 'list' object has no attribute 'items'
Its array of objects
Let's say output
looks like this:
output = [{'title': 'title 1\u200c', 'subject': 'subject1\u200c'},
{'title': 'title 2\u200c', 'subject': 'subject2\u200c'}]
You want to do what I showed above to each dict in output
. Just replace output
from before with elem
for elem in output:
elem['title'] = elem['title'].replace("\u200c", "")
[{'title': 'title 1', 'subject': 'subject1\u200c'},
{'title': 'title 2', 'subject': 'subject2\u200c'}]
Or, using a list and dict comprehension:
output = [
{key: value.replace("\u200c", "") for key, value in elem.items()}
for elem in output
]
[{'title': 'title 1', 'subject': 'subject1'},
{'title': 'title 2', 'subject': 'subject2'}]