Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing \xa0 from string in a list

I have a list with a bunch of words:

lista = ['Jeux Olympiques De Rio\xa02016', 'Sahara Ray', 'Michael Phelps', 'Amber Alert']

I tried to replace the '\xa0':

for element in listor:
    element = element.replace('\xa0',' ')

But it didn't work. Also, when I print the elements, it prints:

print(lista[0])
Jeux Olympiques De Rio 2016

Does anyone have an idea on how to solve this?

like image 281
imfromsweden Avatar asked Dec 04 '25 21:12

imfromsweden


2 Answers

the easiest way:

lista = [el.replace('\xa0',' ') for el in lista]
like image 130
Sergey Luchko Avatar answered Dec 06 '25 10:12

Sergey Luchko


for index, element in enumerate(listor):
    listor[index] = element.replace('\xa0',' ')

Now you're replacing the string within the list rather than trying to change the string itself. Previously, you were assigning a new string to the same variable name, which doesn't change the previous string but just changes the string that the "element" variable is pointing to. With this, you'll be actually overwriting the list element with a new string.

like image 38
James Avatar answered Dec 06 '25 09:12

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!