Skip to content

Instantly share code, notes, and snippets.

@ojas
Created September 18, 2013 22:02
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 ojas/6616389 to your computer and use it in GitHub Desktop.
Save ojas/6616389 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import json
CHUNK_SIZE = 500
def chunks(l, n):
""" Yield successive n-sized chunks from array l
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def file_chunks(file_lines, n):
""" Yield successive n-sized chunks from file-like object
"""
arr = []
for line in file_lines:
arr.append(line)
if len(arr)>=n:
yield arr
arr = []
if len(arr):
yield arr
with open('duns-2.json', 'rb') as f:
duns_list = json.load(f)
print len(duns_list)
duns_arr = [e[0] + '\n' for e in duns_list]
with open('duns-2.txt', 'wb+') as f:
for line in duns_arr:
f.writelines(line)
s = 0
with open('duns-2.txt', 'rb') as f:
for chunk in file_chunks(f, CHUNK_SIZE):
s += len(chunk)
print s
s = 0
with open('duns-2.txt', 'rb') as f:
for idx, chunk in enumerate(file_chunks(f, CHUNK_SIZE)):
s += len(chunk)
print s
s = 0
for chunk in chunks(duns_list, CHUNK_SIZE):
s += len(chunk)
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment