简体   繁体   English

Python csv reader for row in reader给出语法错误

[英]Python csv reader for row in reader gives syntax error

New to Django/Python. 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).我需要为 CSV 文件编写一个导入脚本来播种一些数据(不使用固定装置,因为那是基于 JSON 而不是 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?那么第二版有什么问题呢?

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],
              )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM