How to get only numbers from a string

On various Hardware and OS systems: pi / windows / routers / nas, etc

Moderator: leecollings

Post Reply
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: How to get only numbers from a string

Post by EddyG »

Use a split function with a space as delimiter.

Code: Select all

function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: How to get only numbers from a string

Post by MikeF »

In Python, you can just do this (in your specific case):

Code: Select all

Data = parsedJson["result"][1]["Data"][:-4]
which will drop off the last 4 characters.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get only numbers from a string

Post by waaren »


Steven29 wrote: How to get rid of the kWh ? So the outcome will be 2.700
Use

Code: Select all

print("Data:" .. Data:match('%d+'))
in Lua / dzVents


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest