How to remove this \xa0 from a string in python?

后端 未结 5 1103
心在旅途
心在旅途 2020-12-15 09:07

I have the following string:

 word = u\'Buffalo,\\xa0IL\\xa060625\'

I don\'t want the \"\\xa0\" in there. How can I get rid of it? The st

5条回答
  •  攒了一身酷
    2020-12-15 09:24

    If you know for sure that is the only character you don't want, you can .replace it:

    >>> word.replace(u'\xa0', ' ')
    u'Buffalo, IL 60625'
    

    If you need to handle all non-ascii characters, encoding and replacing bad characters might be a good start...:

    >>> word.encode('ascii', 'replace')
    'Buffalo,?IL?60625'
    

提交回复
热议问题