0

I have a string, such as u'jdiajsi--dasdsa\xa0'. I need to get rid of \xao and obtain u'jdiajsi--dasdsa'.

First, I tried str.replace("\xa0", ", "), but it does not work. Then, I change to re.sub(r'\W+', ' ', str), but the result is "jdiajsi dasdsa". The two dashes are gone...

str = "jdiajsi--dasdsa\xa0"
str.replace("\xa0", ", ") #fail
re.sub(r'\W+', ' ', str)  #fail

The first result is no change, the second one miss --. The expected result is "jdiajsi--dasdsa"

The input is unicode rather than a string

  • 1
    `.replace` returns a new string, also if you should not use str and finally do `s = s.replace("\xa0", "")` – Dani Mesejo Sep 18 '19 at 23:57
  • Hi, it's possible to use hex codes in python strings. The string can be replaced with: `newString = oldString.replace('\xa0', '')` – IronMan Sep 19 '19 at 00:00

1 Answers1

1

Strings are immutable; none of their methods, nor any function, can change them in place, they always return a new string. You need to assign them to something to see the changed version, e.g.:

mystr = "jdiajsi--dasdsa\xa0"
mystr = mystr.replace("\xa0", ", ")
mystr = re.sub(r'\W+', ' ', mystr)

Note that I changed the name of the variable; naming a variable str cuts off access to the str constructor, which will eventually byte you.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • sorry, I made a mistake. The type of input is a unicode rather than a string. I guess this is the reason replace method not work – askquestion628 Sep 19 '19 at 00:11