Skip to content

Instantly share code, notes, and snippets.

@isagalaev
Created November 27, 2014 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isagalaev/ab627d235082e20a0062 to your computer and use it in GitHub Desktop.
Save isagalaev/ab627d235082e20a0062 to your computer and use it in GitHub Desktop.
float-wrapper
import ijson
def parse(events):
for prefix, event, value in events:
if event == 'number':
value = float(value)
yield prefix, event, value
f = urllib.request.urlopen('...')
for obj in ijson.common.items(parse(ijson.parse(f)), '... prefix ...'):
handle(obj)
@dmulfinger
Copy link

dmulfinger commented Feb 15, 2018

So I really like ijson, except for the Decimal problem. While I appreciate the desire to maximize precision, I'm stumped by non-equivalent serializing/deserializing of floating point values.

A couple questions here:

  1. Since you're doing ijson.parse(f), doesn't that read the whole file into memory?
  2. This is not working on my python 2.7 installation. When I try this (code below), I do not get any items. Any ideas?

`
import ijson

def parse(events):
for prefix, event, value in events:
if event == 'number':
value = float(value)
yield prefix, event, value

def handle_msg(obj):
print obj

file = open("testfile.txt", "w")
file.write('[ { "fval1" : 37.5 }, { "fval2" : 41.9 }, { "fval3" : 77.2 } ]')
file.close()

f = open('testfile.txt')
for obj in ijson.common.items(parse(ijson.parse(f)), '... prefix ...'):
print 'got object: %s' % obj
handle_msg(obj)

print "Done"

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment