3

I hope to remove \xao in the word of python list. Each word is a unicode type.

I have used replace method to get rid of \xa0, but it does not work.

list = [u'apple\xa0', u'banana']

list = [el.replace('\xa0',' ') for el in list]

print list

The expected res is:

list = [u'apple', u'banana']

The actual res is:

 UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128)
lenik
  • 23,228
  • 4
  • 34
  • 43
  • 1
    Possible duplicate of [How to replace unicode characters in string with something else python?](https://stackoverflow.com/questions/13093727/how-to-replace-unicode-characters-in-string-with-something-else-python) – Nouman Sep 17 '19 at 01:43
  • 1
    BTW, if you are wanting to _remove_ the character instead of _replacing it with a space_, you will want to make the second argument of the replace `''`. – whydoubt Sep 17 '19 at 01:49

1 Answers1

4

You are missing the u at the beginning of your string:

[el.replace(u'\xa0',' ') for el in list]

I would also avoid using list since it is a built-in function in Python.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228