Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95721
| From | random832@fastmail.us |
|---|---|
| Subject | Re: Casting to a "number" (both int and float)? |
| Date | 2015-08-28 01:54 -0400 |
| References | <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.97.1440741274.11709.python-list@python.org> (permalink) |
On Fri, Aug 28, 2015, at 00:57, Victor Hooi wrote:
> I'm reading JSON output from an input file, and extracting values.
>
> Many of the fields are meant to be numerical, however, some fields are
> wrapped in a "floatApprox" dict, which messed with my parsing.
> Is there a way to re-write strip_floatApprox_wrapping to handle both
> ints/floats, and preserve the original format?
You could do simply isinstance(field, (int, float)).
> Or is there a more elegant way to deal with the arbitrary nesting with
> floatApprox?
I'd probably just recursively walk through the dictionary finding these
and removing them.
def clean_floatapprox(obj):
if type(obj) is dict:
for key, value in obj.items():
if type(value) is dict:
if len(value) == 1 and 'floatApprox' in value:
obj[key] = value['floatApprox']
else
clean_floatapprox(obj)
Since it's a JSON result I didn't bother with making a copy or dealing
with the possibility of circular references for this example function,
so you'll need to add to this if you have to deal with those things.
Incidentally, do you ever get one that represents a very large value and
has more fields than just floatApprox? I googled this and it looks like
it's a MongoDB thing.
The example given is {"floatApprox" : 9223372036854776000,
"top" : 2147483647, "bottom" : 4294967295}
Actually representing the value 9223372036854775807. If top and bottom
are found the result should be top << 32 | bottom. I don't know how
large negative numbers are represented.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-27 21:57 -0700
Re: Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-27 22:04 -0700
Re: Casting to a "number" (both int and float)? "Sven R. Kunze" <srkunze@mail.de> - 2015-08-28 18:09 +0200
Re: Casting to a "number" (both int and float)? Robin Koch <robin.koch@t-online.de> - 2015-08-28 18:15 +0200
Re: Casting to a "number" (both int and float)? "Sven R. Kunze" <srkunze@mail.de> - 2015-08-30 13:38 +0200
Re: Casting to a "number" (both int and float)? Steven D'Aprano <steve@pearwood.info> - 2015-08-30 22:00 +1000
Re: Casting to a "number" (both int and float)? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-30 13:55 +0100
Re: Casting to a "number" (both int and float)? Ben Finney <ben+python@benfinney.id.au> - 2015-08-28 15:44 +1000
Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvestingn@makes.address.invalid> - 2015-08-28 09:03 +0300
Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-08-28 09:15 +0300
Re: Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-28 00:56 -0700
Re: Casting to a "number" (both int and float)? Chris Angelico <rosuav@gmail.com> - 2015-08-28 19:23 +1000
Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-08-28 08:46 +0300
Re: Casting to a "number" (both int and float)? random832@fastmail.us - 2015-08-28 01:54 -0400
Re: Casting to a "number" (both int and float)? Laura Creighton <lac@openend.se> - 2015-08-28 07:59 +0200
csiph-web