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

后端 未结 5 1106
心在旅途
心在旅途 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:22

    There is no \xa there. If you try to put that into a string literal, you're going to get a syntax error if you're lucky, or it's going to swallow up the next attempted character if you're not, because \x sequences aways have to be followed by two hexadecimal digits.

    What you have is \xa0, which is an escape sequence for the character U+00A0, aka "NO-BREAK SPACE".

    I think you want to replace them with spaces, but whatever you want to do is pretty easy to write:

    word.replace(u'\xa0', u' ') # replaced with space
    word.replace(u'\xa0', u'0') # closest to what you were literally asking for
    word.replace(u'\xa0', u'')  # removed completely
    

提交回复
热议问题