Reading JSON files in python -


i have json file looks this

20219 {"topic":"electronics","question":"what effective differencial effective of circuit","excerpt":"i'm trying work out, in general terms, effective capacitance of circuit (see diagram: http://i.stack.imgur.com/bs85b.png).  \n\nwhat effective capacitance of circuit , ...\r\n        "} {"topic":"electronics","question":"heat sensor fan cooling","excerpt":"can know component senses heat or acts heat sensor in following circuit?\nin given diagram, said 4148 diode acts sensor. zener diode , ...\r\n        "} {"topic":"electronics","question":"outlet installation--more wires new outlet can use [on hold]","excerpt":"i replacing wall outlet cooper wiring usb outlet (tr7745).  new outlet has 3 wires coming out of it--a black, white, , green.  each 1 needs attached wire nut ...\r\n        "} {"topic":"electronics","question":"buck converter operation question","excerpt":"i have been reading buck converter, , have referred various online resources here.\n\n\n\nin above circuit, understand, when switch closes, current starts increase ...\r\n        "} {"topic":"electronics","question":"urgent in area of asic design, verification, soc [on hold]","excerpt":"i need deciding on master's project , need ideas related field of asic design/ verification or related soc's, fpga , or combination. wish pursue ...\r\n        "} 

the first line number (=20219), number of records in file followed data. tried using following-

import json open('training_json.json') data_file:         ndocs = json.readlines(data_file)[0] 

and

with open('training_json.json') data_file:     next(data_file)     docs = json.load(data_file) 

but couldn't through. ideas how can read number on first line , data trailing below in different objects?

read first line, send else parsing json.loads():

with open("training_json.json") data_file:     number = next(data_file).strip()     your_json = json.loads(data_file.read()) 

in case have different json object on each line (as appears image), read rest line-by-line , store in list:

with open("training_json.json") data_file:     number = next(data_file).strip()     your_jsons = [json.loads(line) line in data_file] 

if json broken down on lines haphazzardly, you'll have manual reconstruction before parse.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -