Encoding NaN in JSON

John Gordon gordon at panix.com
Wed Apr 17 10:47:27 EDT 2013


In <c37a3b9c-6fe8-48aa-b703-9b4f922c3969 at googlegroups.com> Miki Tebeka <miki.tebeka at gmail.com> writes:

> >> I'm trying to find a way to have json emit float('NaN') as 'N/A'.
> > No.  There is no way to represent NaN in JSON.  It's simply not part of the
> > specification.
> I know that. I'm trying to emit the *string* 'N/A' for every NaN.

import math

x = possibly_NaN()

if math.isnan(x):
    x = 'N/A'

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list