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?

rmcsharry
  • 4,357
  • 4
  • 50
  • 87
  • `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. – Hampus Larsson Apr 15 '20 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. – rmcsharry Apr 15 '20 at 12:42

1 Answers1

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],
              )
rmcsharry
  • 4,357
  • 4
  • 50
  • 87
RMPR
  • 2,848
  • 3
  • 16
  • 26
  • 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" – rmcsharry Apr 15 '20 at 12:31
  • Answer edited you need to remove the trailing characters and fix the indentation – RMPR Apr 15 '20 at 12:38
  • 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? – rmcsharry Apr 15 '20 at 12:39
  • 1
    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](https://stackoverflow.com/a/5893946/8805293). – Hampus Larsson Apr 15 '20 at 12:43
  • @HampusLarsson That leads me to this next question. Perhaps you have time to answer it? https://stackoverflow.com/questions/61229166/python-csv-reader-for-row-in-reader-what-does-the-the-throwaway-underscore-rep – rmcsharry Apr 15 '20 at 12:51