如何读取此JSON文件? [英] How to read this JSON file?

查看:116
本文介绍了如何读取此JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的JSON文件:

I have a JSON file that looks like this:

20219
{"topic":"electronics","question":"What is the effective differencial effective of this circuit","excerpt":"I'm trying to work out, in general terms, the effective capacitance of this circuit (see diagram: http://i.stack.imgur.com/BS85b.png).  \n\nWhat is the effective capacitance of this circuit and will the ...\r\n        "}
{"topic":"electronics","question":"Heat sensor with fan cooling","excerpt":"Can I know which component senses heat or acts as heat sensor in the following circuit?\nIn the given diagram, it is said that the 4148 diode acts as the sensor. But basically it is a zener diode and ...\r\n        "}
{"topic":"electronics","question":"Outlet Installation--more wires than my new outlet can use [on hold]","excerpt":"I am replacing a wall outlet with a Cooper Wiring USB outlet (TR7745).  The new outlet has 3 wires coming out of it--a black, a white, and a green.  Each one needs to be attached with a wire nut to ...\r\n        "}
{"topic":"electronics","question":"Buck Converter Operation Question","excerpt":"i have been reading about the buck converter, and have also referred to the various online resources like here.\n\n\n\nIn the above circuit, as I understand, when switch closes, current starts to increase ...\r\n        "}
{"topic":"electronics","question":"Urgent help in area of ASIC design, verification, SoC [on hold]","excerpt":"I need help with deciding on a Master's Project and I need some ideas related to the field of ASIC Design/ verification or something related to SoC's, FPGA and or combination. I wish to pursue the ...\r\n        "}

第一行是一个数字(20219),基本上是文件中记录的数量,后跟数据.我尝试使用以下内容:

The first line is a number (20219), which is basically number of records in the file is followed by the data. I tried using the following:

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

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

但无法通过.有什么想法可以读取第一行中的数字以及下面在不同对象中尾随的数据吗?

but it couldn't get through. Any ideas how I can read the number on the first line and the data trailing below in different objects?

推荐答案

阅读第一行,然后将其他所有内容发送给json.loads():

Read the first line, then send everything else for parsing to json.loads():

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

如果每行上都有一个不同的JSON对象(从图像中显示),请逐行读取其余的JSON并存储在列表中:

In case you have a different JSON object on each line (as it appears from your image), then read the rest line-by-line and store in a list:

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

如果您的JSON严重损坏了行,则必须先进行一些手动重构,然后才能对其进行解析.

If your JSON is broken down on lines haphazzardly, you'll have to do some manual reconstruction before you get it to parse.

这篇关于如何读取此JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆