【问题标题】:Error with reading JSON file lines in Django app在 Django 应用程序中读取 JSON 文件行时出错
【发布时间】:2020-11-08 16:41:38
【问题描述】:

我有这段代码,但我的 JSON 文件中有一些带有空行的行。我得到这个错误。 这是一个自定义命令,我收到此错误。我想在我的 Django 应用程序的数据库中创建一个作业列表,我正在使用 For 循环。非常感谢您的帮助

 raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
from django.core.management.base import BaseCommand
from jobs.models import Job
import json

class Command(BaseCommand):
    help = 'Set up the database'

    def handle(self, *args: str, **options: str):
        with open('static/joblist03112020.json', 'r') as handle:
            for line in handle.readlines():
                print(line)
                line = json.loads(line)

                existing_job = Job.objects.filter(

                    job_title = line['job_title'],
                    company = line['company'],
                    company_url = line['company_url'],
                    description = line['description'],
                    salary = line['salary'],
                    city = line['city'],
                    district = line['district'],
                    url = line['url'],
                    job_type = line['job_type'],

                )
                if existing_job.exists() is False:
                    Job.objects.create(

                        job_title = line['job_title'],
                        company = line['company'],
                        company_url = line['company_url'],
                        description = line['description'],
                        salary = line['salary'],
                        city = line['city'],
                        district = line['district'],
                        url = line['url'],
                        job_type = line['job_type'],

                    )

                    Job.save()
                    self.stdout.write(self.style.SUCCES('added jobs!add'))

【问题讨论】:

    标签: python json django


    【解决方案1】:

    您可能希望加载整个 JSON 文件,而不是逐行加载。不能保证 JSON 文件中的每一行本身都可以作为有效的 JSON 对象进行解析。

    这是我认为你想做的精神:

    with open('static/joblist03112020.json') as f:
        job_list = json.load(f)
    

    请注意,以'r' 开头进行读取是默认设置,因此我将其删除。

    现在您可以遍历列表中的每个作业,并应用过滤器。如果您的 JSON 对象键与模型的列匹配,您也可以简化该代码:

    for job in job_list:
        existing_job = Job.objects.filter(**job)
        if not existing_job.exists():
            Job.objects.create(**job)
    

    请注意,您无需调用.save(),因为create 包含保存功能。另外,.save() 方法无论如何都只定义在类的实例上。

    【讨论】:

    • ` class Command(BaseCommand): help = 'Set up the database' def handle(self, *args: str, **options: str): with open('static/joblist03112020.json' ) as f: job_list = json.load(f) for job in job_list: existing_job = Job.objects.filter( `like this?
    • 你的意思是匹配我的模型?相同的领域?和相同的字段顺序?
    • 如果字段相同,可以使用字典解包操作(**)将参数传递给filtercreate命令。
    【解决方案2】:

    这个平台之外的人可以帮助我解决我的问题。这就是我所做的。并解决问题。我需要使用大 JSON

     def handle(self, *args: str, **options: str):
                with open('static/data.json', 'r') as handle:
                    big_json = json.loads(handle.read())
                    for item in big_json:
                        existing_job = Job.objects.filter(
    

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多