In [1]:
import csv, re

In [2]:
#count current objects in Book
len(Book.objects.all())


Out[2]:
0

In [3]:
file = "data/zoteroDEFC_export_20160205.csv"

In [4]:
with open(file, 'r', encoding ='utf-8') as data:
    reader = csv.reader(data)
    datalist = list(reader)

In [5]:
failed = []
saved = []
for row in datalist:
    if row[2] != "":
        NewBook = Book(zoterokey = row[0],
                      item_type = row[1],
                      author = row[3],
                      title = row[4],
                      publication_title = row[5],
                      short_title = row[21],
                      place = row[27],
                      publication_year = row[2])
        try:
            NewBook.save()
            saved.append(row)
        except:
            failed.append(row)
    else:
        NewBook = Book(zoterokey = row[0],
                      item_type = row[1],
                      author = row[3],
                      title = row[4],
                      publication_title = row[5],
                      short_title = row[21],
                      place = row[27])
        try:
            NewBook.save()
            saved.append(row)
        except:
            failed.append(row)
print('saved: {} objects \nfailed: {} objects'.format(len(saved), len(failed)))


C:\Anaconda3\envs\defc\lib\site-packages\django\db\backends\mysql\base.py:124: Warning: Data truncated for column 'publication_title' at row 1
  return self.cursor.execute(query, args)

WARNING:py.warnings:C:\Anaconda3\envs\defc\lib\site-packages\django\db\backends\mysql\base.py:124: Warning: Data truncated for column 'publication_title' at row 1
  return self.cursor.execute(query, args)

C:\Anaconda3\envs\defc\lib\site-packages\django\db\backends\mysql\base.py:124: Warning: Data truncated for column 'author' at row 1
  return self.cursor.execute(query, args)

WARNING:py.warnings:C:\Anaconda3\envs\defc\lib\site-packages\django\db\backends\mysql\base.py:124: Warning: Data truncated for column 'author' at row 1
  return self.cursor.execute(query, args)

saved: 6293 objects 
failed: 1 objects

In [121]:
# delete all Book-objects:
#Book.objects.all().delete()


Out[121]:
6293

In [6]:
failed


Out[6]:
[['\ufeff"Key"',
  'Item Type',
  'Publication Year',
  'Author',
  'Title',
  'Publication Title',
  'ISBN',
  'ISSN',
  'DOI',
  'Url',
  'Abstract Note',
  'Date',
  'Date Added',
  'Date Modified',
  'Access Date',
  'Pages',
  'Num Pages',
  'Issue',
  'Volume',
  'Number Of Volumes',
  'Journal Abbreviation',
  'Short Title',
  'Series',
  'Series Number',
  'Series Text',
  'Series Title',
  'Publisher',
  'Place',
  'Language',
  'Rights',
  'Type',
  'Archive',
  'Archive Location',
  'Library Catalog',
  'Call Number',
  'Extra',
  'Notes',
  'File Attachments',
  'Link Attachments',
  'Manual Tags',
  'Automatic Tags',
  'Editor',
  'Series Editor',
  'Translator',
  'Contributor',
  'Attorney Agent',
  'Book Author',
  'Cast Member',
  'Commenter',
  'Composer',
  'Cosponsor',
  'Counsel',
  'Interviewer',
  'Producer',
  'Recipient',
  'Reviewed Author',
  'Scriptwriter',
  'Words By',
  'Guest',
  'Number',
  'Edition',
  'Running Time',
  'Scale',
  'Medium',
  'Artwork Size',
  'Filing Date',
  'Application Number',
  'Assignee',
  'Issuing Authority',
  'Country',
  'Meeting Name',
  'Conference Name',
  'Court',
  'References',
  'Reporter',
  'Legal Status',
  'Priority Numbers',
  'Programming Language',
  'Version',
  'System',
  'Code',
  'Code Number',
  'Section',
  'Session',
  'Committee',
  'History',
  'Legislative Body']]

In [ ]: