Thanks for your response

I am using djjango-extensions to run the script. Everything imports fine except 
the one field. The Category, Region, County and Iso’s are foreign keys to the 
property.

The data in the csv for the area_hectares does contain in some cases no data 
but the format for the data is:
158.9265
70
58.9

150
7200000
665.03

Here is the contents of my import script

import csv  # https://docs.python.org/3/library/csv.html

# python manage.py runscript import_data_csv

from unesco.models import Category, County, Region, Iso, Property


def run():
    # file handler
    fhand = open('unesco/whc-sites.csv')
    reader = csv.reader(fhand)
    next(reader)  # Advance past the header

    contSuccess = 0
    Category.objects.all().delete()
    County.objects.all().delete()
    Region.objects.all().delete()
    Iso.objects.all().delete()
    Property.objects.all().delete()

    for row in reader:
        print(row)

        category_object, created = 
Category.objects.get_or_create(name=str(row[7]))
        county_object, created = States.objects.get_or_create(name=str(row[8]))
        region_object, created = Region.objects.get_or_create(name=str(row[9]))
        iso_object, created = Iso.objects.get_or_create(name=str(row[10]))
        e, created = Property.objects.get_or_create(name=row[0], 
description=row[1], sale_details=row[2], year=int(row[3]), 
longtitude=float(row[4]), latitude=float(row[5]), area_hectares=float(row[6]), 
category=category_object, county=county_object, region=region_object, 
iso=iso_object)

        contSuccess += 1
    print(f'{str(contSuccess)} inserted successfully! ')


> On 16 Oct 2019, at 11:49, Kasper Laudrup <[email protected]> wrote:
> 
> Hi dtdave,
> 
> On 16/10/2019 12.28, 'dtdave' via Django users wrote:
>> In my script for importing from a csv I seem to run into the following error:
>> could not convert string to float
>> The relevant part of the script is as follows:
>> area_hectares=float(row[6])
> 
> That is impossible to tell without seeing the contents of your CSV file, but 
> obviously row 6 of that file at least sometimes contains something that 
> cannot be converted to a float.
> 
> I'd suggest you simply print out the values of row 6 in your script to see 
> which values you cannot convert and then handle that. Possibly it's an empty 
> string, in which case you might want to handle that by converting that to a 
> NoneType, but that depends on your use case of course.
> 
> Kind regards,
> 
> Kasper Laudrup
> 
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "Django users" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/django-users/vlHtz3JtsC0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/6360fe4d-e3f2-6825-e73a-ca93084667ae%40stacktrace.dk.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8809DB93-AABC-4B0D-A0E6-75F05C5C54E4%40googlemail.com.

Reply via email to