0

I am trying to read a json file in python but unable to do it. My json file is 436mb in size and I am using python 3.3.4 version Tried different ways

import json
from pprint import pprint
with open('drug-event-0002-of-0002.json') as data_file:    
    data = json.load(data_file)
print(data)

Error :
Traceback (most recent call last):
  File "C:/Users/533335/Desktop/jsonReadTest.py", line 6, in <module>
    print(data)
  File "C:\Python33\lib\idlelib\PyShell.py", line 1339, in write
    return self.shell.write(s, self.tags)
MemoryError

---------------------------------------------------------------------   
import ijson
with open('drug-event-0002-of-0002.json', 'r') as f:
    parser = ijson.parse(f)
    for prefix, event, value in parser:
        if prefix == "name":
            print(value)

Error :
Traceback (most recent call last):
  File "C:/Users/533335/Desktop/jsonReadTest.py", line 1, in <module>
    import ijson
ImportError: No module named 'ijson'
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
Adarsh
  • 83
  • 1
  • 2
  • 9
  • 7
    It seems like your first try worked, it just can't print it. Try printing while iterating over the dictionary. – Dean Fenster Jul 13 '16 at 07:02
  • 1
    [ijson](https://pypi.python.org/pypi/ijson/) certainly looks like it might fit the bill. Have you installed it (it's not part of the standard library)? – SiHa Jul 13 '16 at 07:19
  • 1
    SiHa : I am a beginner in python. So if you guide me on how to install ijson to python, it would be great. Even previously i have been trying installing other packages like NLTK but could not do it. – Adarsh Jul 13 '16 at 07:47
  • @Dean: I tried this now import json f = open('drug-event-0002-of-0002.json', 'r') json.load(f) print(f) f.close() and got one line output <_io.TextIOWrapper name='drug-event-0002-of-0002.json' mode='r' encoding='cp1252'> And how to iterate the first code over the dictionary? – Adarsh Jul 13 '16 at 07:49
  • Try this: `with open('drug-event-0002-of-0002.json') as data_file:` `data = json.load(data_file)` `for key,value in data.items():` `print(key,value)` – Dean Fenster Jul 13 '16 at 07:51
  • tried it : results Traceback (most recent call last): File "C:/Users/533335/Desktop/jsonReadTest.py", line 5, in print(key,value) File "C:\Python33\lib\idlelib\PyShell.py", line 1339, in write return self.shell.write(s, self.tags) MemoryError Got results and then error – Adarsh Jul 13 '16 at 07:55

0 Answers0