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()
您可能还想创建 surv
并将其写入文件,并将其添加到 surveys
,如果您希望 data
中有重复项。
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()
,因为您只需要跟踪标识符,但您将无法访问本节之后的其余调查(与之前不同).
我有一个包含 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()
您可能还想创建 surv
并将其写入文件,并将其添加到 surveys
,如果您希望 data
中有重复项。
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()
,因为您只需要跟踪标识符,但您将无法访问本节之后的其余调查(与之前不同).