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

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

使用 Python 在新的 .json 文件中打印每一行 json

使用 Python 在新的 .json 文件中打印每一行 json

我有一個(gè)json文件;我需要id從內(nèi)容中刪除密鑰,我可以用我的代碼來(lái)做?,F(xiàn)在我想json在一個(gè)新文件中打印文件的每一行,并使用 myjson中的名稱作為文件名。我的json文件前:{"categories":["Test"],"indications":[{"@class":"=indication.BuildLogIndication","pattern":".*TypeError .*"},{"@class":"model.indication.BuildLogIndication","pattern":".*LoadError .*"}],"modifications":[{"time":{"$date":"2015-10-08T20:01:54.075Z"}},{"user":"user1","time":{"$date":"2015-03-04T18:38:58.123Z"}},{"user":"user2","time":{"$date":"2014-11-13T01:54:13.906Z"}},{"time":{"$date":"2014-09-02T18:48:05.000Z"}}],"lastOccurred":{"$date":"2017-01-25T20:05:17.180Z"}}{"pattern":".*look for this string.*"}],"modifications":[{"time":{"$date":"2014-09-02T18:52:20.000Z"}}],"lastOccurred":{"$date":"2014-11-04T00:43:32.945Z"},"_removed":{"timestamp":{"$date":"2014-11-13T01:52:44.346Z"},"by":"user3"},"active":false}刪除id的代碼:import jsonimport sysimport reimport fileinputinfile = "failure.json"outfile = "failure1.json"fin = open(infile)fout = open(outfile, "w+")for line in fin:    for word in line:        line = re.sub("\"_id.*?},","", line)    fout.write(line)    file.write("%d\n" % n)fin.close()fout.close()
查看完整描述

3 回答

?
心有法竹

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

您的示例輸入json在每一行上顯示一個(gè)對(duì)象。


所以我的解決方案讀取每一行并將其轉(zhuǎn)換為python dict(使用json.loads()),從dict(使用dict.pop()如果鍵不存在靜默失?。┲袆h除所需的鍵并將其轉(zhuǎn)換回字符串(使用json.dumps()),然后將其寫(xiě)入新文件。


import json


infile = "failure.json"

outfile = "failure1.json"

key = '_id'


with open(infile) as f_read:

    with open(outfile, 'w') as f_write:

        for line in f_read:

            line = line.strip()

            if len(line) > 0:

                try:

                    elem = json.loads(line)

                    elem.pop(key, None)

                    f_write.write('{}\n'.format(json.dumps(elem)))

                except json.JSONDecodeError:

                    pass

編輯:json根據(jù) OP 的評(píng)論,顯然每一行都應(yīng)該進(jìn)入一個(gè)單獨(dú)的新文件??梢赃@樣做,例如:


import json


infile = "failure.json"

key_to_remove = '_id'


with open(infile) as f_read:

    for line in f_read:

        line = line.strip()

        if len(line) > 0:

            try:

                elem = json.loads(line)

                elem.pop(key_to_remove, None)


                outfile = '{}.json'.format(elem['name'])      # this may raise KeyError

                with open(outfile, 'w') as f_write:

                    f_write.write('{}\n'.format(json.dumps(elem)))

            except json.JSONDecodeError:

                pass


查看完整回答
反對(duì) 回復(fù) 2021-07-06
?
蕪湖不蕪

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

對(duì)于刪除,你可以使用這樣的東西:


import json

import sys

import re

import fileinput


with open('failure.json') as data_file:

    data = json.load(data_file)

    del data['_id']



with open('failure2.json', 'w') as data_file:

    data = json.dump(data, data_file)

并且為了創(chuàng)建具有 id 值的文件,只需解析data對(duì)象和id節(jié)點(diǎn)值


查看完整回答
反對(duì) 回復(fù) 2021-07-06
?
一只斗牛犬

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

您已導(dǎo)入該json包,但并未使用它。你應(yīng)該,這很棒。

從文件中獲取字符串,然后用于json.loads()將字符串加載到 json 對(duì)象中。從那里,您可以使用 .json 獲取 json 對(duì)象的每個(gè)元素for key in json_object


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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