I have a CSV file that my users upload, enter some data and upload to my site.
Is there a better way to ensure successful data loading based on my snippet below? What else should I check? Would using a dialect be better?
def import(resident_file): try: file = resident_file.file.path reader = csv.reader(open(file, 'rU'), delimiter=',', quotechar='"') headerline = reader.next() for row in reader: try:
An example of a problem that I encounter is that when a user opens a file, enters data and saves them, the delimiters change from , to ; . How can I cover the different types of separators so that the document can be saved because it was opened in different programs, for example, Excel in Excel, Excel in Mac, open office on Mac, open office on Linux, etc.
Another example of a problem is when a user tries to copy and paste data into the provided template, all hell breaks.
UPDATE Now I use the Sniffer class as indicated in one of the answers below, but its still not proof of a fool.
UPDATED SNIPET CODE
def bulk_import_residents(condo, resident_file): """ COL 1 COL 2 COL 3 COL 4 COL 5 first_name last_name contact_number unit_number block_number """ file_path = resident_file.file.path csvfile = open(file_path, 'rb') dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) reader = csv.reader(csvfile, dialect) headerline = reader.next() for row in reader: try: data = ResidentImportData() data.condo = condo data.file = resident_file data.first_name = row[0] data.last_name = row[1] data.contact_number = row[2] data.unit_number = row[3] data.block_number = row[4] data.save() except Exception, e: print '{0}'.format(e) raise Http404('Wrong template format')
source share