for循环字典写入json

时间:2020-03-21 15:28:24

标签: python json python-3.x dictionary

我有一个脚本,可以从API中提取信息。 该API是用于职位发布的数据库,我以json的形式接收信息。

然后我给不同的标题,名称,日期一个变量。 变量通过for循环更新并一个接一个地打印,因此我将得到这样的帖子:

ID: 234523
jobtitle
company
deadline
link

ID: 5454554
jobtitle
company
deadline
link

etc.

我现在想要的是将其输出到json文件,以便以后可以比较ID并将新的发布添加到文件中。 我目前遇到的问题是,像这样输出后,database.json文件中的格式已关闭:

for i in jobs:
    employername = i['employer']
    emp_type = i['employment_type']
    fulltime = i['duration']
    jobtitle = i['headline']
    etc.


    output = {
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    }
    with open('pbdb.json', 'a+') as job_data_file:
        json.dump(output, job_data_file, indent=4,)

输出类似于:

{
    "ID": "23961983",
    "Title": "Test",
    "Employer": "comp",
    "Employment type": "regular",
    "Fulltime": "fulltime",
    "Deadline": "2020-09-06",
    "Link": "https://"
}{
    "ID": "23960352",
    "Title": "a job",
    "Employer": "comp2",
    "Employment type": "regular",
    "Fulltime": "4 months",
    "Deadline": "2020-03-27",
    "Link": "https://"
}

,并且在json文件中出现错误,字典和“预期文件结尾”之间没有逗号。有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

您需要将其转储为列表,而不是单个条目:

output = []
for i in jobs:
    ...

    output.append({
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    })

with open('pbdb.json', 'w') as job_data_file:
    json.dump(output, job_data_file)

要使其附加到json文件中:

output = json.load(open("pbdb.json"))

如果文件为空则中断,解决方法:

import os

check_empty = os.stat('pbdb.json').st_size
if check_empty == 0:
    with open('pbdb.json', 'w') as f:
        f.write('[\n]')    # Writes '[' then linebreaks with '\n' and writes ']'
output = json.load(open("pbdb.json"))

for i in jobs:
    output.append({
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    })

with open('pbdb.json', 'w') as job_data_file:
    json.dump(output, job_data_file)