簡體   English   中英

如何閱讀這個JSON文件?

[英]How to read this JSON file?

我有一個看起來像這樣的JSON文件:

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 ),它基本上是文件中的記錄數,后跟數據。 我嘗試使用以下內容:

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)

但它無法通過。 任何想法我如何讀取第一行的數字和下面的數據在不同的對象?

讀取第一行,然后將其他所有內容發送到json.loads()

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

如果你在每一行上有一個不同的JSON對象(從你的圖像中看到它),那么逐行閱讀其余的並存儲在一個列表中:

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

如果您的JSON在線路上被破壞了,那么在進行解析之前,您必須進行一些手動重建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM