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

Grant Edwards grant.b.edwards at gmail.com
Sat Feb 17 15:47:09 EST 2024


On 2024-02-16, Chris Green via Python-list <python-list at python.org> 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'.

It would probably help if you told us how you're "outputting" them now
(the Python feaatures/functions used, not the actual output format).

Are you using f-strings, the % operator, str.format(), or ??

I would be tempted to try monkey-patching the float class to override
the __format__ method. I have no idea what side effects that might
have, or if it's even used by the various formatting mechanisms, so
you might end up scraping bits off the walls...

--
Grant


More information about the Python-list mailing list