Truncate a Variable with Python

Posted on
Thu Aug 16, 2018 11:44 am
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Truncate a Variable with Python

I have a variable that holds wind info. Typically it reads something like WSW at 2 mph.
What I’m after is to shorten it to a single character direction keeping whatever the speed may be...e.g. W at 2 mph.

I thought about just limiting the character length but of course that wouldn’t take into account the wind speed digits.

Any ideas appreciated.

Thanks,

Carl

Posted on
Thu Aug 16, 2018 1:21 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Truncate a Variable with Python

Can you give specific examples of what you get and what you want, including the wind speed portion?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Aug 16, 2018 1:54 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Truncate a Variable with Python

Sure. More often than not it shows a 3 character wind direction, SSE, WSW etc. followed by the wind speed - SSE at 10 mph or whatever.

I have limited space on the CP and would like it to only show a 1 character wind direction followed by wind speed.
So for example an “ESE at 8 mph” would show as “E at 8 mph”.

Thanks,

Carl

Posted on
Thu Aug 16, 2018 2:56 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Truncate a Variable with Python

Wouldn't it make more sense to change "ESE at 10 mph" to "ESE@10" if you're trying to save space?

In any case:

Code: Select all
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
>>> input = "ESE at 10 mph"
>>> words = input.split(' ')
>>> words
['ESE', 'at', '10', 'mph']
>>> short = "{}@{}".format(words[0], words[2])
>>> short
'ESE@10'
>>> short = "{:.1} {} {} {}".format(words[0], words[1], words[2],words[3])
>>> short
'E at 10 mph'
>>>

It's the ".1" that truncates the string to length 1.

This assumes you already know how to get a string from a variable and then update the variable when you're done.
Last edited by FlyingDiver on Thu Aug 16, 2018 6:15 pm, edited 2 times in total.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Aug 16, 2018 5:24 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Truncate a Variable with Python

Thanks a bunch. Gives me a lot to experiment with.

Carl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests