Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import json
  2. import ijson
  3.  
  4.  
  5. #%%
  6.  
  7. #Step 1
  8. #Write items to separate file
  9. from ijson import items
  10.  
  11. with open('item.json', encoding='windows-1251') as file:
  12.     for item in items(file, '_embedded.items.item'):
  13.         with open('clean_items.json', mode='a+', encoding='windows-1251') as f:
  14.             json.dump(item, f)
  15.  
  16.  
  17.    
  18. #%%
  19. #Step 2
  20. #Write separate items to DB
  21.  
  22. from json import JSONDecoder
  23. from functools import partial
  24.  
  25. #following fuction read items one at the time  
  26. def json_parse(fileobj, decoder=JSONDecoder(), buffersize=2048):
  27.     buffer = ''
  28.     for chunk in iter(partial(fileobj.read, buffersize), ''):
  29.          buffer += chunk
  30.          while buffer:
  31.              try:
  32.                  result, index = decoder.raw_decode(buffer)
  33.                  yield result
  34.                  buffer = buffer[index:].lstrip()
  35.              except ValueError:
  36.                  # Not enough data to decode, read more
  37.                  break
  38.  
  39. #%%
  40. #with usage of json_parse function we can write data to DB
  41. #without a lot of a memory consumption
  42.  
  43. with open('clean_items.json', 'r') as infh:
  44.     for data in json_parse(infh):
  45.         #write data to DB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement