0

What is the most efficient way to clean this array ['1\xa0790\xa0000\xa0kr', '1\xa0980\xa0000\xa0kr'] into a new array that looks like this ['1790000', '1980000]?

I am a beginner in python and appreciate any advice, thank you!.

I tried a douple for loop and deleted chars that were equal to "x","a". When trying backslash it failed.

2 Answers2

3

That string does not contain any x's, a's, or backslashes. The string '\xa0' contains one character -- a non-breaking space, with the hex value A0. Use

s = s.replace('\xa0','')

This doesn't help the "0kr" at the end. You can use another replace to get rid of those.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

Re will do the job of cleaning for regex. something below

import re


def clean_array(array):
    # Only number in array
    array = [re.sub(r'\D', '', x) for x in array]
    return array

print(clean_array(['1\xa0790\xa0000\xa0kr', '1\xa0980\xa0000\xa0kr']))```
twister_void
  • 1,377
  • 3
  • 13
  • 31