Help needed removing whitespace from string - weird issue

Hi, using Scrape I get some price as string with whitespace, which I need as float.
How to remove it or convert the price to float?

# ok:
{% set price = '1 199' %}
{{ price }}
{{ price.replace(" ", "") | float}}
# the problem:
{% set price = states.sensor.tv_heureka.state %}
{{ price }}
# price == '1 199'
{{ price.replace(" ", "") }}
{{ price.replace(' ', '') }}
# conversion to float not possible
Result type: string
# ok:
1 199
1199.0

# the problem:
1 199
# price == '1 199'
1 199
1 199
# conversion to float not possible

Try this:

{{ (states('sensor.tv_heuteka') | slugify).replace('_', '') | float(0) }}

I don’t have the means to test it so I can’t guarantee it will work.

It does not work with the second string, but thanks for your effort anyway!

I wish I could be of more help but I can’t simulate the data you have.

I tried this but the   is displayed literally whereas in your example it’s displayed as a space.

{% set price = '1 199' %}
{{ price }}
{{ (price | slugify).replace('_', '') | float(0) }}

That’s true - I can reproduce your output. But when the string value comes from the website it is not working. I will try to analyze and compare both strings if I can see what’s the difference.

perhaps a regex replace sort of

{{ (price) | regex_replace(find='([^0-9].)', replace='') | float(0) }}

additional note: you don’t want to remove what you don’t know, you want to keep what’s wanted. Sort of looking from the other side onto the problem.

or try {{ price|urlencode }} and remove the converted non-breaking space, probably %C2%A0.

1 Like

Thank you, I also thought about using regex. Unfortunatelly I was getting 199 instead of 1199 with that code but after some tweaking I am sure it would be ok.

Thank you it worked!
{{ price|urlencode | replace("%C2%A0", "") }}
gives 1199 as I needed.

1 Like

Good stuff. The regex is also a good plan, although should have been:

{{ (price) | regex_replace(find='[^0-9]', replace='') | float(0) }}

The original version was removing a group of “not-a-digit” followed by anything: in this case our magic space and the following 1.