Can one output something other than 'nan' for not a number values?

Piergiorgio Sartor piergiorgio.sartor.this.should.not.be.used at nexgo.REMOVETHIS.de
Sun Feb 18 09:03:51 EST 2024


On 16/02/2024 23.12, Chris Green wrote:
> I'm looking for a simple way to make NaN values output as something
> like '-' or even just a space instead of the string 'nan'.  This would
> then make it much easier to handle outputting values from sensors when
> not all sensors are present.
> 
> So, for example, my battery monitoring program outputs:-
> 
>      Battery Voltages and Currents
>      Leisure Battery - 12.42 volts  -0.52 Amps
>      Starter Battery - 12.34 volts  -0.01 Amps
> 
> If the starter battery sensor has failed, or is disconnected, I see:-
> 
>      Battery Voltages and Currents
>      Leisure Battery - 12.42 volts  -0.52 Amps
>      Starter Battery -   nan volts    nan Amps
> 
> 
> What I would like is for those 'nan' strings to be just a '-' or
> something similar.
> 
> Obviously I can write conditional code to check for float('nan')
> values but is there a neater way with any sort of formatting string or
> other sort of cleverness?

Uhm, I cannot see how to avoid conditional code.

Somewhere, function, class, method, there should be
an "if isnan(x)".

You can hide that, but you cannot avoid, I suspect.

bye,

-- 

piergiorgio



More information about the Python-list mailing list