0

New to Django/Python. I need to write an import script for a CSV file to seed some data (not using fixtures, did that already as that is JSON based and not CSV).

This works:

import csv
from datetime import datetime
from django.utils.timezone import make_aware
from django.core.management.base import BaseCommand
from chatterbox.models import Organisation, Course, Student

class Command(BaseCommand):
    def handle(self, **options):

      CSV_PATH = './students_AEKI.csv'

      Student.objects.filter(organisation__name__exact="AEKI").delete()

      with open(CSV_PATH) as file:
        file.readline() # skip the header
        csv_reader = csv.reader(file, delimiter=',')
        org = Organisation.objects.filter(name="AEKI")

        for row in csv_reader:
          _, Student.objects.get_or_create(
              first_name=row[0],
              last_name=row[1],
              email=row[2],
              organisation=org[0],
              enrolled=row[4],
              last_booking=row[5],
              credits_total=row[6],
              credits_balance=row[7],
              )

This does NOT work:

import csv
from datetime import datetime
from django.utils.timezone import make_aware
from django.core.management.base import BaseCommand
from chatterbox.models import Organisation, Course, Student

class Command(BaseCommand):
    def handle(self, **options):

      CSV_PATH = './students_AEKI.csv'

      Student.objects.filter(organisation__name__exact="AEKI").delete()

      with open(CSV_PATH) as file:
        file.readline() # skip the header
        csv_reader = csv.reader(file, delimiter=',')
        org = Organisation.objects.filter(name="AEKI")

        for row in csv_reader:
          enrolled_utc = make_aware(datetime.strptime(row[4], '%Y-%m-%d'))
          last_booking_utc = make_aware(datetime.strptime((row[5], '%Y-%m-%d'))
          _, Student.objects.get_or_create(
              first_name=row[0],
              last_name=row[1],
              email=row[2],
              organisation=org[0],
              enrolled=enrolled_utc,
              last_booking=last_booking_utc,
              credits_total=row[6],
              credits_balance=row[7],
              )

Syntax error at the "_".

I need to do some manipulation (eg like adding timezone to date fields) on data before creating it in the table. So what is wrong with the 2nd version?

2
  • last_booking_utc = datetime.strptime((row[5], '%Y-%m-%d') contains an extra (, is this on purpose? Regardless if it's on purpose or not, the problem is probably with unbalanced parenthesis. Commented Apr 15, 2020 at 12:39
  • @HampusLarsson Thanks, yes I just spotted that too! Updated the answer as it seems that the underscore and indentation are also part of the problem. Commented Apr 15, 2020 at 12:42

1 Answer 1

1

There's a Syntax error at the "_". Remove the trailing characters.

Also this line has an extra bracket:

last_booking_utc = datetime.strptime((row[5], '%Y-%m-%d')

From

        for row in csv_reader:
          enrolled_utc = make_aware(datetime.strptime(row[4], '%Y-%m-%d'))
          last_booking_utc = make_aware(datetime.strptime((row[5], '%Y-%m-%d'))
          _, Student.objects.get_or_create(
              first_name=row[0],
              last_name=row[1],
              email=row[2],
              organisation=org[0],
              enrolled=enrolled_utc,
              last_booking=last_booking_utc,
              credits_total=row[6],
              credits_balance=row[7],
              )

To

        for row in csv_reader:
            enrolled_utc = make_aware(datetime.strptime(row[4], '%Y-%m-%d'))
            last_booking_utc = make_aware(datetime.strptime(row[5], '%Y-%m-%d'))
            Student.objects.get_or_create(
              first_name=row[0],
              last_name=row[1],
              email=row[2],
              organisation=org[0],
              enrolled=enrolled_utc,
              last_booking=last_booking_utc,
              credits_total=row[6],
              credits_balance=row[7],
              )
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but that gives "SyntaxError: invalid syntax" - look at my 2nd piece of code. The "Student" does not immediately follow the line "for row in csv_reader"
Answer edited you need to remove the trailing characters and fix the indentation
Thank you but I see the real cause is an extra ( on last_boooking_utc line ((row[5]. What is the purpose of the underscore then?
A singular underscore is often just used as a "placeholder" in certain loops where the variable it holds is mostly discarded. A more complete answer can be found here.
@HampusLarsson That leads me to this next question. Perhaps you have time to answer it? stackoverflow.com/questions/61229166/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.