On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote: > On 16Feb2024 22:12, Chris Green <c...@isbd.net> 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'. [...] >> >> 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. > > The simplest thing is probably just a function writing it how you want > it: > > def float_s(f): > if isnan(f): > return "-" > return str(f)
He's obviouisly using a formatting feature to control columns and decimal places, so I doubt that 'str(f)' is going to meet the need. I tried monkey-patching the __format__ method of the 'float' type, but it's immutable -- so that didn't work. Is float.__format__() what's used by f-strings, the % operator, etc.? -- Grant -- https://mail.python.org/mailman/listinfo/python-list