第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

一個(gè)文件中的多個(gè)Json對(duì)象由python提取

一個(gè)文件中的多個(gè)Json對(duì)象由python提取

我是Json文件的新手。如果我有一個(gè)帶有多個(gè)json對(duì)象的json文件,如下所示:{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",  "Code":[{"event1":"A","result":"1"},…]}{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",  "Code":[{"event1":"B","result":"1"},…]}{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",  "Code":[{"event1":"B","result":"0"},…]}…我想將所有“時(shí)間戳”和“有用性”提取到數(shù)據(jù)框中:    Timestamp    Usefulness 0   20140101      Yes 1   20140102      No 2   20140103      No …有誰知道處理這些問題的一般方法?謝謝!
查看完整描述

3 回答

?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

使用json數(shù)組,格式為:


[

{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",

  "Code":[{"event1":"A","result":"1"},…]},

{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",

  "Code":[{"event1":"B","result":"1"},…]},

{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",

  "Code":[{"event1":"B","result":"0"},…]},

...

]

然后將其導(dǎo)入您的python代碼


import json


with open('file.json') as json_file:


    data = json.load(json_file)

現(xiàn)在,數(shù)據(jù)的內(nèi)容是一個(gè)數(shù)組,其中的字典代表每個(gè)元素。


您可以輕松訪問它,即:


data[0]["ID"]


查看完整回答
反對(duì) 回復(fù) 2019-09-02
?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以使用json.JSONDecoder.raw_decode任意解碼大量“堆疊”JSON字符串(只要它們可以適合內(nèi)存)。raw_decode一旦它有一個(gè)有效的對(duì)象就停止,并返回不在解析對(duì)象中的最后一個(gè)位置。它沒有記錄,但您可以將此位置傳回,raw_decode并從該位置再次開始解析。不幸的是,Python json模塊不接受具有前綴空格的字符串。所以我們需要搜索以找到文檔的第一個(gè)非空白部分。


from json import JSONDecoder, JSONDecodeError

import re


NOT_WHITESPACE = re.compile(r'[^\s]')


def decode_stacked(document, pos=0, decoder=JSONDecoder()):

    while True:

        match = NOT_WHITESPACE.search(document, pos)

        if not match:

            return

        pos = match.start()


        try:

            obj, pos = decoder.raw_decode(document, pos)

        except JSONDecodeError:

            # do something sensible if there's some error

            raise

        yield obj


s = """


{"a": 1}  



   [

1

,   

2

]



"""


for obj in decode_stacked(s):

    print(obj)

打?。?/p>


{'a': 1}

[1, 2]


查看完整回答
反對(duì) 回復(fù) 2019-09-02
  • 3 回答
  • 0 關(guān)注
  • 3311 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)