Skip to content

Instantly share code, notes, and snippets.

@mdengler
Created January 5, 2017 02:27
Show Gist options
  • Save mdengler/63826f02744f23e29389ca5d50eeeb94 to your computer and use it in GitHub Desktop.
Save mdengler/63826f02744f23e29389ca5d50eeeb94 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import json
import random
filename = "test.json"
x = [{"ID": i,
"xxx": "".join(["f"] * random.randint(1, 20)),
"yyy": "".join(["f"] * random.randint(1, 20))}
for i in range(20)]
with open(filename, "wb") as fh:
for d in x:
fh.write(json.dumps(d) + "\n")
X = dict()
with open(filename, 'r') as f:
for line in f:
print(line)
data = json.loads(line)
if data['xxx'] is not None and data['yyy'] is not None:
X[data['ID']] = len(data['xxx']) / len(data['yyy']) # this line is the problem
print(X)
@mdengler
Copy link
Author

mdengler commented Jan 5, 2017

$ ~/tmp/test.py
{"xxx": "ffffffffffffffffffff", "yyy": "fffffffff", "ID": 0}

{"xxx": "ffffffffffffffffffff", "yyy": "ffff", "ID": 1}

{"xxx": "fffffff", "yyy": "ffffffffffffff", "ID": 2}

{"xxx": "ffffffffffffff", "yyy": "ffffffffffffffffff", "ID": 3}

{"xxx": "fffffffff", "yyy": "fffffffffffff", "ID": 4}

{"xxx": "ff", "yyy": "f", "ID": 5}

{"xxx": "ffffffffff", "yyy": "ffffffffffff", "ID": 6}

{"xxx": "fff", "yyy": "fffffffffffffffff", "ID": 7}

{"xxx": "ffffffffffffff", "yyy": "ffffffffffffffff", "ID": 8}

{"xxx": "ffffffffffffff", "yyy": "fffffffffffffffffff", "ID": 9}

{"xxx": "ffffffffffffff", "yyy": "ffffff", "ID": 10}

{"xxx": "ffff", "yyy": "f", "ID": 11}

{"xxx": "fffff", "yyy": "fffffffffffff", "ID": 12}

{"xxx": "fffffffffff", "yyy": "ffffffffffff", "ID": 13}

{"xxx": "fffffffffffffffff", "yyy": "fff", "ID": 14}

{"xxx": "fffff", "yyy": "ffffffffffffffffff", "ID": 15}

{"xxx": "fffff", "yyy": "ffffffffffffffffff", "ID": 16}

{"xxx": "fffffffff", "yyy": "ffffff", "ID": 17}

{"xxx": "ffffffffffff", "yyy": "ffffffffffffff", "ID": 18}

{"xxx": "fffffff", "yyy": "fffffff", "ID": 19}

{0: 2, 1: 5, 2: 0, 3: 0, 4: 0, 5: 2, 6: 0, 7: 0, 8: 0, 9: 0, 10: 2, 11: 4, 12: 0, 13: 0, 14: 5, 15: 0, 16: 0, 17: 1, 18: 0, 19: 1}

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