簡體   English   中英

Python 如何使用換行符讀取字典文件?

[英]Python how can I read file of dictionaries with newline?

我有一個像這樣的 json 對象文件

dict\n
dict\n
.
.
.

這就是我制作這個文件的方式

with open(old_surveys.json, 'a+') as f1:
            for survey in data:
                surv = {"sid": survey["id"],
                    "svy_ttl": survey["title"]),
                    "svy_link": survey["href"]
                    }
                f1.seek(0)
                
                if str(surv["sid"]) not in f1.read():
                    json.dump(surv, f1)
                    f1.write('\n')
            f1.close()

現在我想檢查一個特定的字典是否在old_surveys.json文件中。 如何逐行閱讀?

假設你有這樣的文件

{"sid": 1, "svy_ttl": "foo", "svy_link": "foo.com"}
{"sid": 2, "svy_ttl": "bar", "svy_link": "bar.com"}
{"sid": 3, "svy_ttl": "Alice", "svy_link": "alice.com"}
{"sid": 4, "svy_ttl": "Bob", "svy_link": "bob.com"}

這個代碼片段怎么樣? 我不確定這是最佳解決方案

import json


def target_dict_exists(target_dict, filename):
    with open(filename, "r") as f:
        for line in f:
            if json.loads(line) == target_dict:
                return True
    return False


if __name__ == "__main__":
    target = {"sid": 3, "svy_ttl": "Alice", "svy_link": "alice.com"}
    print(target_dict_exists(target, "test.txt"))

為了以更有效的方式避免重復,並回答您的問題:

import json

with open('old_surveys.json', 'a+') as f1:
    # first load all the old surveys in a dictionary
    f1.seek(0)
    surveys = {}
    for line in f1:
        d = json.loads(line)
        surveys[d['sid']] = d
    # then write any new ones from data
    for survey in data:
        if survey['id'] not in surveys:
            json.dump({'sid': survey['id'], 'svy_ttl': survey['title'], 'svy_link': survey['href']}, f1)
            f1.write('\n')
    # this line is not needed, it closes thanks to with
    # f1.close()

或者,如果您希望在data中重復,您可能仍希望創建surv並將其寫入文件,以及將其添加到surveys中。

import json

with open('old_surveys.json', 'a+') as f1:
    f1.seek(0)
    surveys = {}
    for line in f1:
        d = json.loads(line)
        surveys[d['sid']] = d
    for survey in data:
        if survey["id"] not in surveys:
            surv = {"sid": survey["id"], "svy_ttl": survey["title"], "svy_link": survey["href"]}
            surveys[surv['id']] = surv
            json.dump(surv, f1)
            f1.write('\n')

如果您真的不需要調查,而只需要標識符,則效率更高:

import json

with open('old_surveys.json', 'a+') as f1:
    f1.seek(0)
    surveys = set()
    for line in f1:
        d = json.loads(line)
        surveys.add(d['sid'])
    for survey in data:
        if survey["id"] not in surveys:
            surv = {"sid": survey["id"], "svy_ttl": survey["title"], "svy_link": survey["href"]}
            surveys.add(surv['id'])
            json.dump(surv, f1)
            f1.write('\n')

在這里,字典已被替換為set() ,因為您只需要跟蹤標識符,但您將無法訪問本節之后的調查的 rest(與以前不同)。

暫無
暫無

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

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