在CSV中导入无字段

时间:2019-05-15 21:10:17

标签: python django csv try-except

我在工作中有一个人的历史数据库表,其中包括开始日期和结束日期字段。我是从csv插入的,我的脚本工作正常,除了Python / Django将我的None字段视为“ None”,并且它拒绝插入结束日期为空的行,这意味着所有当前的工作机会都被跳过。尝试/除外失败的这部分除外:

    except ValidationError as v:
        if row['end_date'] == 'null' or 'None' or '':
            row['end_date'] = None

就像这个一样:

    except ValidationError as v:
        if row['end_date'] in ['null', 'None', '']:
            row['end_date'] = None

我通过过滤掉当前的工作机会,然后将他们修改为以下脚本,在单独的csv中运行它们来解决了这个问题:

            end_date=None

但是,我真正需要的是某种使用变量的方法,例如值...

        if row[value] == 'None':
            row[value] = None

因为我要运行更多的csv,除了日期以外,还有其他字段,其中一些也将具有空/无值。

会进行某种字符串替换吗?

        if row[value] == 'None':
            row[value].replace(row[value], None)

,或者也许default = None在我模型的end_date字段中没有定义?但是,如何使Django在CSV中看到默认值而不是空值/无?根据文档,https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.Field.default“在创建新的模型实例且未为该字段提供值时使用默认值。”。但是我不确定这在这里是如何工作的。我会获得默认值,还是会收到ValidationError?

最后,我是否在错误地排除了block之外的位置上进行了修复?除了对try块中的故障发出警告以外,except块可以做什么来解决这些限制?

谢谢。

编辑-代码-

#! usr/local/bin/python3.7
# coding: utf-8

import csv
import sys
import logging

from os import environ
import django
sys.path.append('/home/malikarumi/Projects/hattie/hattie')

environ['DJANGO_SETTINGS_MODULE'] = 'hattie.settings'
django.setup()

from people.models import Company, Associate, CareerHistory
from django.utils.text import slugify
from django.db import IntegrityError
from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType


# logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('2019-05-13_inserts.txt')
logger.addHandler(file_handler)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)

stream_handler = logging.StreamHandler()
logger.addHandler(stream_handler)
stream_handler.setFormatter(formatter)


with open('members6') as csvfile:
    reader = csv.DictReader(csvfile, delimiter='\t')
    fieldnames = [
        'last_name', 'first_name', 'middle_name', 'suffix',
        'title', 'start_date', 'end_date'
    ]
    pc = ContentType.objects.get_for_model(Associate)
    org = ContentType.objects.get_for_model(Company)
    for row in reader:
        try:
            name_string = row['last_name'], row['first_name'], row['middle_name'], row['suffix']
            associate_instance = Associate.objects.create(
                last_name=row['last_name'], first_name=row['first_name'],
                middle_name=row['middle_name'], suffix=row['suffix'],
                slug=slugify(name_string))
            CareerHistory.objects.create(
                content_type_pc=pc, object_id_pc=associate_instance.uniqid,
                content_type_org=org, object_id_org="828f4116-38eb-4f0c-9c5a-96a93682d106",
                title=row['title'], start_date=row['start_date'],
                end_date=row['end_date']
            )
        except IntegrityError as e:
            logger.warning(f"Check for dupes: {name_string}, {e}")
            continue
        except ValidationError as v:
            if row['end_date'] in ['null', 'None', '']:
                row['end_date'] = None


p.s. This also gets a name error:
        value = row[f'{value}']

编辑-csv

last_name   first_name  middle_name suffix  slug    title   start_date  end_date
Clarke  John    Hessin          Associate   1916-10-09  1922-09-18
Sutherland  George              Associate   1922-10-02  1938-01-17
Butler  Pierce              Associate   1923-01-02  1939-11-16
Sanford Edward  Terry           Associate   1923-02-19  1930-03-08
Roberts Owen    Josephus            Associate   1930-06-02  1945-07-31
Murphy  Frank               Associate   1940-02-05  1949-07-19
Byrnes  James   Francis         Associate   1941-07-08  1942-10-03
Jackson Robert  Houghwout           Associate   1941-07-11  1954-10-09
Rutledge    Wiley   Blount          Associate   1943-02-15  1949-09-10
Burton  Harold  Hitz            Associate   1945-10-01  1958-10-13
Clark   Tom Campbell            Associate   1949-08-24  1967-06-12
Minton  Sherman             Associate   1949-10-12  1956-10-15
Whittaker   Charles Evans           Associate   1957-03-25  1962-03-31
Paul John    Stevens            Associate   1975-12-19  2010-06-29
O’Connor    Sam  David         Associate   1981-09-25  2006-01-31
Thomas  Charles                Associate   1991-10-23  None
Green    Rayann    B.           Associate   1993-08-10  None
Branson  Stephen H.          Associate   1994-08-03  None
Allen   Samuel  A.  Jr.     Associate   2006-01-31  None
Sanchez   Sonia               Associate   2009-08-08  None
Kammisch   Elaine               Associate   2010-08-07  None
Gormley Nathan    M.          Associate   2017-04-10  None
King   Brad   M.          Associate   2018-10-06  None

1 个答案:

答案 0 :(得分:0)

不是您收盘后的样子,而是相反-像这样:

if row[value]:
    #I have a value 
else:
   #row[value] doesn't exist

或者只是处理不存在的是:

if not row[value]:
   #row[value] doesn't exist